File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ # ์ค๋์ ๋ฐฐ์ด 2์ง์ ๊ตฌํ๋ ๋ฐฉ๋ฒ์ ๊ทธ๋๋ก ์ ์ฉ.
2+ class Solution :
3+ def hammingWeight (self , n : int ) -> int :
4+ cnt = 0
5+ while n > 0 :
6+ remainder = n % 2
7+ if remainder == 1 :
8+ cnt += 1
9+ n = n // 2
10+ return cnt
11+
12+ # LLM ์ด ์๋ ค์ค ๋ค๋ฅธ ํ์ด.
13+ # ๊ทธ๋ ์ง๋ง ๋ผ์ด๋ธ์ฝ๋ฉํ
์คํธ ๋ฑ์์๋ ์ฉ ์ ํฉํ์ง ์์๋ฏ.
14+ class Solution :
15+ def hammingWeight (self , n : int ) -> int :
16+ return bin (n ).count ('1' )
17+
18+ # ๋ ๋ค๋ฅธ ํ์ด. ๋นํธ์ฐ์ฐ ํ์ฉ.
19+ class Solution :
20+ def hammingWeight (self , n : int ) -> int :
21+ count = 0
22+ while n :
23+ count += n & 1 # ๋งจ ์ค๋ฅธ์ชฝ ๋นํธ๊ฐ 1์ธ์ง ํ์ธ
24+ n >>= 1 # ์ค๋ฅธ์ชฝ์ผ๋ก ํ ์นธ ์ํํธ
25+ return count
Original file line number Diff line number Diff line change 1+ # two point ํ์ด.
2+ # ์๊ฐ๋ณต์ก๋: O(n)
3+ # ์ด์ค๋ฃจํ๋ผ์ ์๊ฐ๋ณต์ก๋ ๊ณ์ฐ์ด ์กฐ๊ธ ํท๊ฐ๋ฆฐ๋ค.
4+ # ์ถ๊ฐ๊ณต๊ฐ์ ์ฐ์ง ์๋๋ค.
5+ class Solution :
6+ def isPalindrome (self , s : str ) -> bool :
7+ leftCursor = 0
8+ rightCursor = len (s ) - 1
9+
10+ while leftCursor < rightCursor :
11+ while leftCursor < rightCursor and not s [leftCursor ].isalnum ():
12+ leftCursor += 1
13+
14+ while leftCursor < rightCursor and not s [rightCursor ].isalnum ():
15+ rightCursor -= 1
16+
17+ if s [leftCursor ].lower () != s [rightCursor ].lower ():
18+ return False
19+
20+ leftCursor += 1
21+ rightCursor -= 1
22+
23+ return True
24+
25+ # ๋ค๋ฅธ ํ์ด.
26+ # ๋ฌธ์์ด ํด๋ฆฌ๋ & ๋ค์ง์ด์ ๋๋ฑ๋น๊ต๋ฅผ ํจ.
27+ # ๊ฐ๊ฒฐํ์ง๋ง ์ถ๊ฐ๊ณต๊ฐ ์ฌ์ฉ -> ๊ณต๊ฐ O(n)
28+ class Solution :
29+ def isPalindrome (self , s : str ) -> bool :
30+ cleaned = '' .join (c .lower () for c in s if c .isalnum ())
31+ return cleaned == cleaned [::- 1 ]
You canโt perform that action at this time.
0 commit comments