We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent efe5619 commit 7be5ae7Copy full SHA for 7be5ae7
Bitwise Operations/338_Counting_Bits.java
Dynamic Programming/338_Counting_Bits.java
@@ -1,16 +1,15 @@
1
class Solution {
2
public int[] countBits(int num) {
3
- int[] dp = new int[num + 1];
4
- int offset = 1;
+ int[] result = new int[num + 1];
5
6
for (int i = 1; i <= num; i++) {
7
- if (offset * 2 == i) {
8
- offset *= 2;
+ if (i % 2 == 0) {
+ result[i] = result[i >> 1];
+ } else {
9
+ result[i] = result[i - 1] + 1;
10
}
-
11
- dp[i] = dp[i - offset] + 1;
12
13
14
- return dp;
+ return result;
15
16
0 commit comments