Skip to content

Commit 16b9a30

Browse files
committed
solve counting bits
1 parent 1511cd0 commit 16b9a30

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

counting-bits/sora0319.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int[] countBits(int n) {
3+
int[] bits = new int[n + 1];
4+
int i = 1;
5+
6+
for (int num = 1; num <= n; num++) {
7+
if (i << 1 == num) {
8+
i = num;
9+
}
10+
bits[num] = 1 + bits[num - i];
11+
}
12+
13+
return bits;
14+
}
15+
}

0 commit comments

Comments
 (0)