class Solution {
public:
    bool hasAlternatingBits(int n) {        
        int last = -1;
        while (n)
        {
            int x = n & 1;
            if (last == -1)
            {
                last = x;                
            }
            else
            {
                if (x == last)
                {
                    return false;
                }
                else
                {
                    last = x;
                }
            }            
            n >>= 1;//n右移1位
        }
        return true;
    }
};

 

posted on 2018-09-29 11:06  Sempron2800+  阅读(78)  评论(0编辑  收藏  举报