Power of Two

来源:互联网 发布:pano2vr player.js 编辑:程序博客网 时间:2024/06/10 01:12
Given an integer, write a function to determine if it is a power of two.
class Solution(object):    def isPowerOfTwo(self, n):        """        :type n: int        :rtype: bool        """        # return n == 2**(len(bin(n)) - 3)        if n == 1:            return True        bin_n = bin(n)        if bin_n[2] == '1' and bin_n[3:] == '0' * (len(bin_n) - 3):            return True        return False


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


0 0