600. 不含连续1的非负整数
为保证权益,题目请参考 600. 不含连续1的非负整数(From LeetCode).
解决方案1
Python
python
# 600. 不含连续1的非负整数
# https://leetcode-cn.com/problems/non-negative-integers-without-consecutive-ones/
################################################################################
class Solution:
def findIntegers(self, n: int) -> int:
dp = [0] * 34
dp[0] = 1
dp[1] = 1
for i in range(2, 34):
dp[i] = dp[i - 1] + dp[i - 2]
res = 0
pre = 0
t = 1 << 31
for i in range(32):
if t & n:
res += dp[32 - i]
if pre == 1:
break
pre = 1
else:
pre = 0
t = t >> 1
if i == 31:
res += 1
return res
################################################################################
if __name__ == "__main__":
solution = Solution()
print(solution.findIntegers(5))
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
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