Skip to content

Commit 04b2d9d

Browse files
committed
solve: Number of 1 Bits
1 parent f5e314c commit 04b2d9d

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed
Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
"""
22
Constraints:
3-
-
3+
- 1 <= n <= 2^31 - 1
44
5-
Time Complexity:
6-
-
5+
Time Complexity: O(k)
6+
- ์—ฌ๊ธฐ์„œ k๋Š” ์ž…๋ ฅ์˜ ๋น„ํŠธ ์ˆ˜
7+
- ์ด ๊ฒฝ์šฐ 32๋น„ํŠธ ์ •์ˆ˜์ด๋ฏ€๋กœ ์‹ค์งˆ์ ์œผ๋กœ O(1) (ํ•ญ์ƒ ์ตœ๋Œ€ 32๋ฒˆ ๋ฐ˜๋ณตํ•˜๊ธฐ ๋•Œ๋ฌธ)
78
8-
Space Complexity:
9-
-
10-
11-
ํ’€์ด ๋ฐฉ๋ฒ•:
12-
-
13-
14-
Further Consideration:
15-
-
9+
Space Complexity: O(1)
10+
- count ๋ณ€์ˆ˜๋งŒ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ์ƒ์ˆ˜ ๊ณต๊ฐ„ ๋ณต์žก๋„
1611
"""
12+
class Solution:
13+
def hammingWeight(self, n: int) -> int:
14+
count = 0
15+
while n:
16+
count += n & 1
17+
n >>= 1
18+
return count
19+
1720

0 commit comments

Comments
ย (0)