File tree Expand file tree Collapse file tree 2 files changed +5
-33
lines changed
main/java/com/fishercoder/solutions
test/java/com/fishercoder Expand file tree Collapse file tree 2 files changed +5
-33
lines changed Original file line number Diff line number Diff line change 22
22
*/
23
23
public class _600 {
24
24
25
- public static class DPSolution {
25
+ public static class Solution1 {
26
26
/**
27
27
* Credit: https://leetcode.com/articles/non-negative-integers-without-consecutive-ones/#approach-3-using-bit-manipulation-accepted
28
28
*/
@@ -53,27 +53,4 @@ public int findIntegers(int num) {
53
53
}
54
54
}
55
55
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
-
79
56
}
Original file line number Diff line number Diff line change 10
10
* Created by fishercoder on 5/28/17.
11
11
*/
12
12
public class _600Test {
13
- private static _600 test ;
14
- private static _600 .DPSolution dpSolution ;
13
+ private static _600 .Solution1 solution1 ;
15
14
16
15
@ BeforeClass
17
16
public static void setup () {
18
- test = new _600 ();
19
- dpSolution = new _600 .DPSolution ();
17
+ solution1 = new _600 .Solution1 ();
20
18
}
21
19
22
20
@ Test
23
21
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 ));
29
24
}
30
25
}
You can’t perform that action at this time.
0 commit comments