1. 程式人生 > >leetcode693+判斷二進位制是否相連不同數字

leetcode693+判斷二進位制是否相連不同數字

https://leetcode.com/problems/binary-number-with-alternating-bits/description/

class Solution {
public:
    bool hasAlternatingBits(int n) {
        int flag = n%2;
        n = n/2;
        while (n>0) {
            int tmp = n%2;
            if(tmp==flag) return false;
            flag = tmp;
            n = n/2;
        }
        return true;
    }
};