版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013554860/article/details/83512916
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;
}
};