File tree Expand file tree Collapse file tree 2 files changed +20
-1
lines changed
Expand file tree Collapse file tree 2 files changed +20
-1
lines changed Original file line number Diff line number Diff line change 1+ """
2+ ๋ฌธ์ ์์ฝ
3+ - 32๋นํธ ๋ถํธ ์๋ ์ ์ n์ ์ด์ง ํํ์์ '1'์ ๊ฐ์(= set bits)๋ฅผ ๊ตฌํ๋ผ.
4+
5+ ์์ด๋์ด A: ๋ธ๋ผ์ด์ธ ์ปค๋ํธ(Brian Kernighan) ์๊ณ ๋ฆฌ์ฆ
6+ - ํต์ฌ ๊ด์ฐฐ: n & (n - 1)์ n์ ์ตํ์ 1๋นํธ๋ฅผ '0'์ผ๋ก ๋ง๋ ๋ค.
7+ ์) n = 0b101100 โ n-1 = 0b101011 โ n & (n-1) = 0b101000 (์ตํ์ 1 ํ๋ ์ญ์ )
8+ - ๋ฐ๋ผ์ '1'์ ๊ฐ์๋งํผ ๋ฃจํ๊ฐ ๋๋ค.
9+ - ์๊ฐ๋ณต์ก๋: O(k) (k = n์ 1๋นํธ ๊ฐ์) โ ๋งค์ฐ ๋น ๋ฆ
10+ - ๊ณต๊ฐ๋ณต์ก๋: O(1)
11+ """
12+
13+ class Solution :
14+ def hammingWeight (self , n : int ) -> int :
15+ cnt = 0
16+ while n :
17+ n &= n - 1 # ์ตํ์ 1๋นํธ ์ ๊ฑฐ
18+ cnt += 1
19+ return cnt
Original file line number Diff line number Diff line change 1212class Solution :
1313 def isPalindrome (self , s : str ) -> bool :
1414 cleaned = [c for c in s .lower () if c .isalnum ()]
15- return cleaned == cleaned [::- 1 ]
15+ return cleaned == cleaned [::- 1 ]
You canโt perform that action at this time.
0 commit comments