-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path693.py
49 lines (36 loc) · 977 Bytes
/
693.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
class Solution:
def hasAlternatingBits(self, n):
"""
:type n: int
:rtype: bool
"""
if n==1:
return True
if n==2:
return True
if n==3:
return False
p = n
result = []
while p !=0:
getbit = p%2
result.append(getbit)
p=p//2
#print(result)
flag=0
for i in range(len(result)):
if i==0 or i==len(result)-1:
continue
#print(result[i],result[i-1],result[i+1])
if result[i]!=result[i-1] and result[i]!=result[i+1]:
flag =0
else:
flag=1
break
if flag==0:
return True
else:
return False
if __name__=='__main__':
sl =Solution()
print(sl.hasAlternatingBits(3))