[leetcode]693. Binary Number with Alternating Bits

[leetcode]693. Binary Number with Alternating Bits


Analysis

ummmm~—— [ummmm~]

Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values.
简单~

Implement

class Solution {
public:
    bool hasAlternatingBits(int n) {
        vector<int> bi;
        while(n){
            bi.push_back(n%2);
            n /= 2;
        }
        int len = bi.size();
        for(int i=1; i<len; i++){
            if(bi[i] == bi[i-1])
                return false;
        }
        return true;
    }
};

猜你喜欢

转载自blog.csdn.net/weixin_32135877/article/details/81122893
今日推荐