Skip to content

Commit 8a3affc

Browse files
refactor 600
1 parent 7401a04 commit 8a3affc

File tree

2 files changed

+5
-33
lines changed

2 files changed

+5
-33
lines changed

src/main/java/com/fishercoder/solutions/_600.java

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*/
2323
public class _600 {
2424

25-
public static class DPSolution {
25+
public static class Solution1 {
2626
/**
2727
* Credit: https://leetcode.com/articles/non-negative-integers-without-consecutive-ones/#approach-3-using-bit-manipulation-accepted
2828
*/
@@ -53,27 +53,4 @@ public int findIntegers(int num) {
5353
}
5454
}
5555

56-
/**
57-
* Brute force is definitely correct, but too time consuming and resulted in TLE.
58-
*/
59-
public int findIntegers(int num) {
60-
int answer = 0;
61-
for (int i = 0; i <= num; i++) {
62-
if (hasConsecutiveOnes(i)) {
63-
answer++;
64-
}
65-
}
66-
return answer;
67-
}
68-
69-
private boolean hasConsecutiveOnes(int num) {
70-
String bin = Integer.toBinaryString(num);
71-
for (int i = 0; i < bin.length() - 1; i++) {
72-
if (bin.charAt(i) == '1' && bin.charAt(i + 1) == '1') {
73-
return false;
74-
}
75-
}
76-
return true;
77-
}
78-
7956
}

src/test/java/com/fishercoder/_600Test.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,16 @@
1010
* Created by fishercoder on 5/28/17.
1111
*/
1212
public class _600Test {
13-
private static _600 test;
14-
private static _600.DPSolution dpSolution;
13+
private static _600.Solution1 solution1;
1514

1615
@BeforeClass
1716
public static void setup() {
18-
test = new _600();
19-
dpSolution = new _600.DPSolution();
17+
solution1 = new _600.Solution1();
2018
}
2119

2220
@Test
2321
public void test1() {
24-
assertEquals(5, dpSolution.findIntegers(5));
25-
assertEquals(514229, dpSolution.findIntegers(100000000));
26-
27-
assertEquals(5, test.findIntegers(5));
28-
assertEquals(514229, test.findIntegers(100000000));//this takes too long when using brute force
22+
assertEquals(5, solution1.findIntegers(5));
23+
assertEquals(514229, solution1.findIntegers(100000000));
2924
}
3025
}

0 commit comments

Comments
 (0)