[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;
}
};