Skip to content

Commit 2e8c147

Browse files
authored
Merge pull request neetcode-gh#45 from FelixRodriguezJr/patch-2
Create 338-Counting-Bits.java
2 parents b200e95 + 9b935f9 commit 2e8c147

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

java/338-Counting-Bits.java

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

0 commit comments

Comments
 (0)