Skip to content

Commit 3bb95fc

Browse files
committed
solve problem
1 parent ff118a8 commit 3bb95fc

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

counting-bits/delight010.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
func countBits(_ n: Int) -> [Int] {
3+
// Time O(n log n)
4+
// Space O(1)
5+
var result: [Int] = []
6+
var count = 0
7+
var num = 0
8+
9+
while num <= n {
10+
var temp = num
11+
12+
while temp != 0 {
13+
if temp & 1 == 1 {
14+
count += 1
15+
}
16+
temp >>= 1
17+
}
18+
19+
result.append(count)
20+
count = 0
21+
num += 1
22+
}
23+
24+
return result
25+
}
26+
}
27+

0 commit comments

Comments
 (0)