Skip to content

Commit 158c096

Browse files
committed
counting bits solve
1 parent 0e32552 commit 158c096

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

counting-bits/JustHm.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// time: O(n)
2+
// approach - 2자리 3자리 ... 로 2진법 변환시 맨 앞 1은 고정이다.
3+
// 그 이후 나머지는 0~N자리일때 총 1의 갯수 를 더해줬다.
4+
class Solution {
5+
func countBits(_ n: Int) -> [Int] {
6+
var answer: [Int] = [0, 1]
7+
8+
while answer.count < n + 1 {
9+
answer += answer.map{$0 + 1}
10+
}
11+
12+
return [Int](answer.prefix(n + 1))
13+
}
14+
}

0 commit comments

Comments
 (0)