题目链接:https://leetcode-cn.com/problems/power-of-four/
参考题目解题链接:(刷题Leetcode231 2 的幂) https://blog.youkuaiyun.com/ZZZ___bj/article/details/117402516?spm=1001.2014.3001.5501
题解思路:
普通思想(使用循环):
将“刷题Leetcode231 2 的幂”中2换为4,对4取余,对4整除
进阶思想(不使用循环):
1.判断是否是2的幂(&)
2. 4的幂的独特之处(对3取mod必为1)
普通题解代码:
def isPowerOfTwo(n):
if(n <= 0):
return False
while(n > 1):
if (n % 4):
return False
n = n / 4
return True
进阶题解代码:
def isPowerOfTwo(n):
if n <= 0:
return False
if(n & (n - 1) == 0 and n % 3 == 1):
return True
return False