231. 2的幂

 

231. 2的幂

方法一

class Solution(object):
    def isPowerOfTwo(self, n):
        """
        :type n: int
        :rtype: bool
        """
        return n > 0 and not (n & (n - 1))

"""
class Solution:
    def isPowerOfTwo(self, n):
        """
        :type n: int
        :rtype: bool
        """
        if n<=0:
            return False
        return n&(n-1) == 0

"""

 

原文地址:https://www.cnblogs.com/xiao-xue-di/p/10292252.html