Skip to content
LC-0693 Easy LeetCode

693. Binary Number with Alternating Bits

Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 63% Topics: Bit Manipulation
View full problem on LeetCode

Reading material

Reference solution (spoiler · python)
# 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

Solution from kamyu104/LeetCode-Solutions · MIT

Similar questions