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 231. Power of Two 2的幂 python 多种思路,一行代码 (位运算&math)
猜你喜欢
转载自blog.csdn.net/huhehaotechangsha/article/details/80895123
今日推荐
周排行