We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent eedb8e6 commit c16c8b3Copy full SHA for c16c8b3
number-of-1-bits/shinsj4653.py
@@ -1,13 +1,53 @@
1
"""
2
[문제풀이]
3
# Inputs
4
-
+- 양인 정수 n
5
# Outputs
6
+- number of set bits => 이진수 값의 1 개수
7
# Constraints
8
+- 1 <= n <= 2^31 - 1
9
# Ideas
10
+1. 반복문
11
+2로 나눠서 몫 나머지 -> 나머지가 1인지 0인지 체크
12
+그 몫을 또 2로 나눠서 몫, 나머지 ->
13
+
14
15
+11
16
+2-> 5, 1
17
+2-> 2, 1
18
+2-> 1, 0
19
+2-> 0, 1
20
21
+몫이 0이 될 때 까지 반복
22
23
+TC: log2N? SC: 1
24
25
[회고]
26
+시간 복잡도가 log2N인가?
27
+-> O
28
29
+해설은 어떤지 궁금
30
+->
31
32
+"""
33
34
+class Solution:
35
+ def hammingWeight(self, n: int) -> int:
36
+ ret = 0
37
+ while n > 0:
38
+ n, bit = n // 2, n % 2
39
+ if bit == 1:
40
+ ret += 1
41
42
+ return ret
43
44
+# 해설
45
46
47
48
+ cnt = 0
49
+ while n:
50
+ cnt += n & 1
51
+ n >>= 1
52
+ return cnt
53
-"""
0 commit comments