题目链接:https://leetcode.com/problems/power-of-two/
代码
class Solution:
def isPowerOfTwo(self, n: int) -> bool:
if n==0:return False
return not n&(n-1)
解题思路
2 n 2^n 2n在二进制上只有一位是1,所有可以从位运算,判断1的个数只有一个即可。
题目链接:https://leetcode.com/problems/power-of-two/
class Solution:
def isPowerOfTwo(self, n: int) -> bool:
if n==0:return False
return not n&(n-1)
2 n 2^n 2n在二进制上只有一位是1,所有可以从位运算,判断1的个数只有一个即可。