Python solution


  • 0
    Y
        def hasAlternatingBits(self, n):
            """
            :type n: int
            :rtype: bool
            """
            x,lastbit=n/2,n%2
            if n==1: return True
            while x>0:
                rest=x%2
                if rest!=lastbit:
                    lastbit=rest
                else:
                    return False
                x/=2
            return True

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.