forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_600.java
36 lines (33 loc) · 986 Bytes
/
_600.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.fishercoder.solutions;
public class _600 {
public static class Solution1 {
/**
* Credit: https://leetcode.com/articles/non-negative-integers-without-consecutive-ones/#approach-3-using-bit-manipulation-accepted
*/
public int findIntegers(int num) {
int[] f = new int[32];
f[0] = 1;
f[1] = 2;
for (int i = 2; i < f.length; i++) {
f[i] = f[i - 1] + f[i - 2];
}
int i = 30;
int sum = 0;
int prevBit = 0;
while (i >= 0) {
if ((num & (1 << i)) != 0) {
sum += f[i];
if (prevBit == 1) {
sum--;
break;
}
prevBit = 1;
} else {
prevBit = 0;
}
i--;
}
return sum + 1;
}
}
}