Skip to content

Commit bbf56ba

Browse files
refactor 338
1 parent e565826 commit bbf56ba

File tree

2 files changed

+51
-32
lines changed

2 files changed

+51
-32
lines changed
Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.fishercoder.solutions;
22

3-
import com.fishercoder.common.utils.CommonUtils;
4-
53
/**338. Counting Bits
64
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array.
75
@@ -24,45 +22,39 @@ Space complexity should be O(n).
2422
*
2523
*/
2624
public class _338 {
27-
private class DPSolution {
28-
//lixx2100's post is cool:https://discuss.leetcode.com/topic/40162/three-line-java-solution
29-
//An easy recurrence for this problem is f[i] = f[i / 2] + i % 2
30-
//and then we'll use bit manipulation to express the above recursion function
31-
// right shift by 1 means to divide by 2
32-
//AND with 1 means to modulo 2
33-
//this is so cool!
25+
public static class Solution1 {
26+
//use the most regular method to get it AC'ed first
3427
public int[] countBits(int num) {
3528
int[] ones = new int[num + 1];
36-
for (int i = 1; i <= num; i++) {
37-
ones[i] = ones[i >> 1] + (i & 1);
29+
for (int i = 0; i <= num; i++) {
30+
ones[i] = countOnes(i);
3831
}
3932
return ones;
4033
}
41-
}
4234

43-
44-
//use the most regular method to get it AC'ed first
45-
public int[] countBits(int num) {
46-
int[] ones = new int[num + 1];
47-
for (int i = 0; i <= num; i++) {
48-
ones[i] = countOnes(i);
35+
private int countOnes(int i) {
36+
int ones = 0;
37+
while (i != 0) {
38+
ones++;
39+
i &= (i - 1);
40+
}
41+
return ones;
4942
}
50-
return ones;
5143
}
5244

53-
private int countOnes(int i) {
54-
int ones = 0;
55-
while (i != 0) {
56-
ones++;
57-
i &= (i - 1);
45+
private class Solution2 {
46+
/**lixx2100's post is cool:https://discuss.leetcode.com/topic/40162/three-line-java-solution
47+
An easy recurrence for this problem is f[i] = f[i / 2] + i % 2
48+
and then we'll use bit manipulation to express the above recursion function
49+
right shift by 1 means to divide by 2
50+
AND with 1 means to modulo 2
51+
this is so cool!*/
52+
public int[] countBits(int num) {
53+
int[] ones = new int[num + 1];
54+
for (int i = 1; i <= num; i++) {
55+
ones[i] = ones[i >> 1] + (i & 1);
56+
}
57+
return ones;
5858
}
59-
return ones;
60-
}
61-
62-
public static void main(String... strings) {
63-
_338 test = new _338();
64-
int num = 15;
65-
int[] ones = test.countBits(num);
66-
CommonUtils.printArray(ones);
6759
}
6860
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.utils.CommonUtils;
4+
import com.fishercoder.solutions._338;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertArrayEquals;
9+
10+
public class _338Test {
11+
private static _338.Solution1 test;
12+
private static int[] expected;
13+
private static int[] actual;
14+
15+
@BeforeClass
16+
public static void setup() {
17+
test = new _338.Solution1();
18+
}
19+
20+
@Test
21+
public void test1() {
22+
expected = new int[] {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
23+
actual = test.countBits(15);
24+
CommonUtils.printArray(actual);
25+
assertArrayEquals(expected, actual);
26+
}
27+
}

0 commit comments

Comments
 (0)