693. Binary Number with Alternating Bits
Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values.
Example 1:
Input: 5
Output: True
Explanation:
The binary representation of 5 is: 101
Example 2:
Input: 7
Output: False
Explanation:
The binary representation of 7 is: 111.
Example 3:
Input: 11
Output: False
Explanation:
The binary representation of 11 is: 1011.
Example 4:
Input: 10
Output: True
Explanation:
The binary representation of 10 is: 1010.
Solution
class Solution:
def hasAlternatingBits(self, n: int) -> bool:
last_bit = n & 1
while n > 0:
n >>= 1
if n & 1 == last_bit:
return False
last_bit = n & 1
return True
This one is pretty straightforward. We iterate through each of the bits of the input number, starting with the least significant bit. We keep track of the last bit we saw, and compare it to the current. If the last bit we saw is the same as the current, we know that it is not alternating.
We use two bit operations here, the bitwise AND
and SHIFT
:
n & 1
performs bitwise AND on the operands and returns the least significant bit in nn >> 1
performs a left bitwise shift, which essentially divides the number by two and removes the last bit- Example:
1010011 >> 1 => 101001
- Example: