Skip to content

[693] Binary Number with Alternating Bits

https://leetcode.com/problems/binary-number-with-alternating-bits/description/

  • algorithms
  • Easy (56.89%)
  • Source Code: 693.binary-number-with-alternating-bits.py
  • Total Accepted: 39.5K
  • Total Submissions: 68.4K
  • Testcase Example: '5'

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.

python
class Solution(object):
    def hasAlternatingBits(self, n):
        """
        :type n: int
        :rtype: bool
        """
        astr = bin(n)
        for idx, c in enumerate(astr[1:], 1):
            if astr[idx] == astr[idx-1]: return False

        return True

Last updated: