5-line Java, beat 54%


  • 0
    A
    public boolean hasAlternatingBits(int n) {
        while(n!=0){
            if( ((n & 1) ^ (n>>>1 & 1)) ==0 ) return false;    
            n >>>= 1;
        }
        return true;        
    }

Log in to reply
 

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