Skip to content

Commit b8b94a3

Browse files
committed
feat: number-of-1-bits 풀이 추가
1 parent 737b202 commit b8b94a3

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

number-of-1-bits/unpo88.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution:
2+
def hammingWeight(self, n: int) -> int:
3+
return bin(n).count('1')
4+
5+
6+
"""
7+
================================================================================
8+
풀이 과정
9+
================================================================================
10+
11+
1. 일단 이진법으로 변경이 필요하고
12+
2. 변경된 이진법에서 1을 카운팅해야하네?
13+
14+
15+
[1차 시도] 내장 함수 활용
16+
────────────────────────────────────────────────────────────────────────────────
17+
3. Python의 bin() 함수로 이진 문자열 변환
18+
4. count() 메서드로 '1' 문자 개수 세기
19+
20+
return bin(n).count('1')
21+
22+
5. 시간 복잡도: O(log n) - 이진 표현의 비트 수만큼
23+
6. 공간 복잡도: O(log n) - 이진 문자열 생성
24+
"""

0 commit comments

Comments
 (0)