Skip to content

Commit dc63da3

Browse files
committed
DaleStudy#233 Counting Bits
1 parent 2dc0827 commit dc63da3

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

counting-bits/forest000014.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
# Time Complexity: O(n)
3+
# Space Complexity: O(1)
4+
*/
5+
class Solution {
6+
public int[] countBits(int n) {
7+
int[] ans = new int[n + 1];
8+
9+
for (int i = 0; i <= n; i++) {
10+
int x = i;
11+
while (x > 0) {
12+
ans[i] += (x & 1);
13+
x >>= 1;
14+
}
15+
}
16+
17+
return ans;
18+
}
19+
}

0 commit comments

Comments
 (0)