Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
non-negative-integers-without-consecutive-ones.py 614 Bytes
一键复制 编辑 原始数据 按行查看 历史
Allen Liu 提交于 2018-10-12 01:33 +08:00 . remove sensitive question description
# Time: O(1)
# Space: O(1)
class Solution(object):
def findIntegers(self, num):
"""
:type num: int
:rtype: int
"""
dp = [0] * 32
dp[0], dp[1] = 1, 2
for i in xrange(2, len(dp)):
dp[i] = dp[i-1] + dp[i-2]
result, prev_bit = 0, 0
for i in reversed(xrange(31)):
if (num & (1 << i)) != 0:
result += dp[i]
if prev_bit == 1:
result -= 1
break
prev_bit = 1
else:
prev_bit = 0
return result + 1
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助