Skip to content

Commit 47cdcb0

Browse files
authored
counting bits solution
1 parent 367a862 commit 47cdcb0

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

counting-bits/yhkee0404.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution {
2+
func countBits(_ n: Int) -> [Int] {
3+
var ans = Array(repeating: 0, count: n + 1) // S(n) = O(n)
4+
for i in 1..<n+1 { // T(n) = O(n)
5+
ans[i] = ans[i >> 1] + (i & 1)
6+
}
7+
return ans
8+
}
9+
}

0 commit comments

Comments
 (0)