【原题链接】
方法1 O(n)
class Solution(object):
def isPowerOfTwo(self, n):
if n == 0:
return False
while n % 2 == 0:
n /= 2
return n == 1
方法2 位操作 O(1)
class Solution:
def isPowerOfTwo(self, n: int) -> bool:
if n <= 0:
return False
return n&(n-1) == 0