题目分析:直接位运算判断即可
C++:
bool isPowerOfFour(int num) {
if (num <= 0)
return false;
if ((num & num - 1) != 0)
return false;
return (num & 0x55555555) == num; //判断是否是4的幂
}
python:
def isPowerOfFour(self, num: int) -> bool:
if num <= 0:
return False
elif (num & num - 1) != 0:
return False
elif num & 0x55555555 == num:
return True