给定一个整数,编写一个函数来判断它是否是 2 的幂次方。
示例 1:
输入: 1 输出: true 解释: 20 = 1
示例 2:
输入: 16 输出: true 解释: 24 = 16
示例 3:
输入: 218 输出: false
import math
class Solution(object):
def isPowerOfTwo(self, n):
"""
:type n: int
:rtype: bool
"""
b = str(bin(n)[2:])
return b.count('1') == 1 and b.count('0') == (len(b) - 1)
def isPowerOfTwo2(self, n):
"""
:type n: int
:rtype: bool
"""
if n <= 0:
return False
k = int(math.log(n, 2))
return 2 ** k == n
def isPowerOfTwo3(self, n):
"""
:type n: int
:rtype: bool
"""
if n == 0: return False
n = n & (n - 1)
if n == 0:
return True
return False
# return n > 0 and not n & (n - 1)
if __name__=='__main__':
s = Solution()
print(s.isPowerOfTwo(16))