leetcode693

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;
    }
};
原文地址:https://www.cnblogs.com/asenyang/p/9722625.html