Skip to content

Commit 76ac42c

Browse files
committed
solve 1
1 parent 344e9d5 commit 76ac42c

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

β€Žnumber-of-1-bits/pmjuu.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'''
2+
μ‹œκ°„ λ³΅μž‘λ„
3+
- format(n, 'b'): μ •μˆ˜λ₯Ό 이진 λ¬Έμžμ—΄λ‘œ λ³€ν™˜ν•˜λŠ” μž‘μ—…μ€ O(k)μž…λ‹ˆλ‹€.
4+
- Counter(bits): λ¬Έμžμ—΄μ„ μˆœνšŒν•˜λ©΄μ„œ 각 문자의 λΉˆλ„λ₯Ό κ³„μ‚°ν•˜λ©°, 이 μž‘μ—…λ„ λ¬Έμžμ—΄ 길이 k에 λΉ„λ‘€ν•©λ‹ˆλ‹€.
5+
- count['1']: λ”•μ…”λ„ˆλ¦¬ μ‘°νšŒλŠ” μƒμˆ˜ μ‹œκ°„μ΄λ―€λ‘œ O(1)μž…λ‹ˆλ‹€.
6+
7+
총 μ‹œκ°„ λ³΅μž‘λ„: O(k) + O(k) + O(1) = O(k)
8+
9+
곡간 λ³΅μž‘λ„
10+
- format(n, 'b'): μƒμ„±λœ 이진 λ¬Έμžμ—΄μ€ 길이 kλ₯Ό μ°¨μ§€ν•©λ‹ˆλ‹€.
11+
- Counter(bits): λ”•μ…”λ„ˆλ¦¬ ν˜•νƒœλ‘œ 각 문자의 λΉˆλ„λ₯Ό μ €μž₯ν•©λ‹ˆλ‹€. μ΅œμ•…μ˜ 경우, 두 κ°€μ§€ 문자(β€˜0’과 β€˜1’)만 μžˆμœΌλ―€λ‘œ 곡간 λ³΅μž‘λ„λŠ” O(2) = O(1)둜 κ°„μ£Όν•  수 μžˆμŠ΅λ‹ˆλ‹€.
12+
13+
총 곡간 λ³΅μž‘λ„: O(k)
14+
'''
15+
16+
from collections import Counter
17+
18+
class Solution:
19+
def hammingWeight(self, n: int) -> int:
20+
bits = format(n, 'b')
21+
count = Counter(bits)
22+
23+
return count['1']

0 commit comments

Comments
Β (0)