We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 216abc0 commit 352cb5eCopy full SHA for 352cb5e
binary-number-with-alternating-bits.py
@@ -4,5 +4,5 @@ def hasAlternatingBits(self, n):
4
:type n: int
5
:rtype: bool
6
"""
7
- bits = bin(n)[2:]
8
- return all(bits[i] != bits[i + 1] for i in range(len(bits) - 1))
+ a = n ^ (n >> 1)
+ return a & (a + 1) == 0
0 commit comments