所有Leetcode题目不定期汇总在 Github, 欢迎大家批评指正,讨论交流。
class Solution:
def isPowerOfTwo(self, n):
"""
:type n: int
:rtype: bool
"""
# once method
# while int(n) == n and n != 0:
# if n == 1:
# return True
# n /= 2
# return False
# twice method
# if n:
# while n % 2 == 0 :
# n /= 2
# return n == 1
# return False
# third method 简洁高效的位运算
return n > 0 and not (n & n-1)
所有Leetcode题目不定期汇总在 Github, 欢迎大家批评指正,讨论交流。