Skip to content

Commit af285fb

Browse files
committed
Create f-exuan21.java
Counting bits solutions
1 parent 20f4909 commit af285fb

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

counting-bits/f-exuan21.java

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

0 commit comments

Comments
 (0)