原题链接https://leetcode.com/problems/power-of-two/
Given an integer, write a function to determine if it is a power of two.
直接看所给整数中位中1的个数 超过2个 就不是2的幂
class Solution {
public:
bool isPowerOfTwo(int n) {
int cnt = 0;
if (n == 0)return false;
while (n)
{
if (n & 1)cnt++;
if (cnt >= 2)return false;
n >>= 1;
}
return true;
}
};