Skip to content

Commit 3f583cc

Browse files
Sean PrashadSean Prashad
authored andcommitted
Add 338_Counting_Bits.java
1 parent 7450db9 commit 3f583cc

File tree

1 file changed

+16
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)