https://leetcode.com/contest/leetcode-weekly-contest-53/problems/binary-number-with-alternating-bits/
class Solution {
public:
bool hasAlternatingBits(int n) {
n = (n >> 1) ^ n;
return ((((n + 1) & n) == 0) ? true : false);
}
};
留言
張貼留言