Skip to content

Commit dbefdfa

Browse files
committed
counting-bits solution
1 parent 0dc18a6 commit dbefdfa

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

counting-bits/yyyyyyyyyKim.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def countBits(self, n: int) -> List[int]:
3+
# 시간복잡도 O(n), 공간복잡도 O(n)
4+
5+
# 0으로 초기화
6+
answer = [0]*(n+1)
7+
8+
for i in range(1,n+1):
9+
# i//2(마지막비트를제외한부분)의 1의 개수 + i의 마지막비트(홀수면 1, 짝수면 0)
10+
answer[i] = answer[i//2] + (i&1)
11+
12+
return answer

0 commit comments

Comments
 (0)