forked from Sunchit/Coding-Decoded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNonNegativeIntegerswithoutConsecutiveOnes.java
66 lines (53 loc) · 1.31 KB
/
NonNegativeIntegerswithoutConsecutiveOnes.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// This is C++ Solution
// Please feel free to add java solution
class Solution {
public:
int findIntegers(int n) {
int f[31];
f[0] = 1;
f[1] = 2;
for (int i = 2; i < 31; ++i)
f[i] = f[i-1]+f[i-2];
int ans = 0, k = 30, pre_bit = 0;
while (k >= 0) {
if (n&(1<<k)) {
ans += f[k];
if (pre_bit) return ans;
pre_bit = 1;
}else {
pre_bit = 0;
}
--k;
}
return ans+1;
}
};
// This is Java Solution
class Solution {
public int findIntegers(int n) {
int[] f = new int[31];
int res = 0;
f[0] = 1;
f[1] = 2;
for(int i = 2; i < 31; i++){
f[i] = f[i-1] + f[i-2];
}
String binaryStr = getBinary(n);
for(int i = 0; i < binaryStr.length(); i++){
if(binaryStr.charAt(i) == '0')
continue;
res += f[binaryStr.length() - i - 1];
if(i != 0 && binaryStr.charAt(i-1) == '1')
return res;
}
return res+1;
}
public String getBinary(int num){
StringBuilder sb = new StringBuilder();
while(num > 0){
sb.insert(0, num&1);
num >>= 1;
}
return sb.toString();
}
}