aboutsummaryrefslogtreecommitdiff
path: root/non_neg_int_wo_consec_one.py
blob: e5c8b8078c1c964b393730d572f500df6e19c551 (plain)
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
# 600. Non-negative Integers without Consecutive Ones

"""
given a positive integer 'n', return the number of the integers in the range
'[0, n]' whose binary representations do not contain consecutive ones.
"""


class Solution(object):
    def findIntegers(self, n):
        """
        :type n: int
        :rtype: int
        """
        s = bin(n + 1)[2:]
        num = len(s)
        dp = [1, 2] + [0] * (num - 2)
        for i in range(2, num):
            dp[i] = dp[i - 1] + dp[i - 2]
        flag, ans = False, 0
        for i in range(num):
            if s[i] == "0":
                continue
            if flag:
                break
            if i > 0 and s[i - 1] == "1":
                flag = True
            ans += dp[-i - 1]
        return ans


if __name__ == "__main__":
    obj = Solution()
    print(obj.findIntegers(n=5))
    print(obj.findIntegers(n=1))
    print(obj.findIntegers(n=2))