Skip to content

Commit 7be5ae7

Browse files
Sean PrashadSean Prashad
authored andcommitted
Update 338_Counting_Bits.java
1 parent efe5619 commit 7be5ae7

File tree

2 files changed

+6
-23
lines changed

2 files changed

+6
-23
lines changed

Bitwise Operations/338_Counting_Bits.java

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
class Solution {
22
public int[] countBits(int num) {
3-
int[] dp = new int[num + 1];
4-
int offset = 1;
3+
int[] result = new int[num + 1];
54

65
for (int i = 1; i <= num; i++) {
7-
if (offset * 2 == i) {
8-
offset *= 2;
6+
if (i % 2 == 0) {
7+
result[i] = result[i >> 1];
8+
} else {
9+
result[i] = result[i - 1] + 1;
910
}
10-
11-
dp[i] = dp[i - offset] + 1;
1211
}
1312

14-
return dp;
13+
return result;
1514
}
1615
}

0 commit comments

Comments
 (0)