c++ solution easy to understand


  • 0
    Q
    class Solution {
    public:
        bool hasAlternatingBits(int n) {
            int lb;
            lb = n&1;
            n = n>>1;
            while(n>0){
                if((n & 1) == 1){
                    if(lb == 1)
                        return false;
                    lb = 1;
                }
                else{
                    if(lb == 0)
                        return false;
                    lb = 0;
                }
                n = n>>1;
            }
            return true;
        }
    };
    

Log in to reply
 

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