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 2222 */
2323public 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}
Original file line number Diff line number Diff line change 1010 * Created by fishercoder on 5/28/17.
1111 */
1212public 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}
You can’t perform that action at this time.
0 commit comments