We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent f897aec commit 96020a9Copy full SHA for 96020a9
โnumber-of-1-bits/Seoya0512.pyโ
@@ -0,0 +1,20 @@
1
+'''
2
+Approach
3
+- ์ญ์ง๋ฒ์ ์ด์ง๋ฒ์ผ๋ก ๋ณํํ๋ ๋ฐฉ์๊ณผ ๋์ ํฉ์ ์ฌ์ฉํจ
4
+
5
+Time Complexity: O(log n)
6
+- while ๋ฌธ์์ ์ซ์(num)์ ๊ณ์ํด์ 2๋ก ๋๋๋๋ฐ ์์๋๋ ์๊ฐ
7
8
+Space Complexity: O(1)
9
+- ์์ bits์ nums๋ฅผ ์ ์ฅํ๋ ๊ณต๊ฐ
10
11
+class Solution:
12
+ def hammingWeight(self, n: int) -> int:
13
+ bits = 0
14
+ # ์ฃผ์ด์ง ์ซ์๋ฅผ 2๋ก ๋๋ ๋๋จธ์ง ๊ฐ์ด 1์ธ ๊ฒฝ์ฐ bits์ ๋์ ํจ
15
+ num = n
16
+ while num != 0 :
17
+ if num % 2 == 1 :
18
+ bits += 1
19
+ num //= 2
20
+ return bits
0 commit comments