Binary Number with Alternating Bits(Java Solution beats 100%)


  • 0
    K
    class Solution {
        public boolean hasAlternatingBits(int n) {
            int last=n%2,cur;
            n=n/2;
            while(n>0){
                cur=n%2;
                if(cur==last) return false;
                last=cur;
                n=n/2;
            }
            return true;
        }
    }
    

Log in to reply
 

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