-
Notifications
You must be signed in to change notification settings - Fork 43
/
binary-number-with-alternating-bits.py
64 lines (60 loc) · 1.6 KB
/
binary-number-with-alternating-bits.py
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
# V0
# V1
# https://blog.csdn.net/fuxuemingzhu/article/details/79089937
# IDEA : GREEDY
# TO CHECK IF 0 ALWAYS NEXT/BEFORE 1 (1 ALWAYS NEXT/BEFORE 0)
# IDEA : ALL FUN IN PYTHON
# https://www.programiz.com/python-programming/methods/built-in/all
# Return Value from all()
# The all() method returns:
# True - If all elements in an iterable are true
# False - If any element in an iterable is false
class Solution(object):
def hasAlternatingBits(self, n):
"""
:type n: int
:rtype: bool
"""
bin_n = bin(n)[2:]
return all(bin_n[i] != bin_n[i+1] for i in range(len(bin_n) - 1))
# V1'
# https://blog.csdn.net/fuxuemingzhu/article/details/79089937
# IDEA : PATTERN
class Solution(object):
def hasAlternatingBits(self, n):
"""
:type n: int
:rtype: bool
"""
b = 0b1010101010101010101010101010101010101010101010101010101010101010
while b > 0:
if b == n:
return True
b = b >> 1
return False
# V1''
# https://blog.csdn.net/fuxuemingzhu/article/details/79089937
# IDEA : BIT MANIPULATION
class Solution(object):
def hasAlternatingBits(self, n):
"""
:type n: int
:rtype: bool
"""
n ^= (n >> 1)
return not (n & n + 1)
# V2
# Time: O(1)
# Space: O(1)
class Solution(object):
def hasAlternatingBits(self, n):
"""
:type n: int
:rtype: bool
"""
n, curr = divmod(n, 2)
while n > 0:
if curr == n % 2:
return False
n, curr = divmod(n, 2)
return True