1. 程式人生 > >leetcode 231. Power of Two

leetcode 231. Power of Two

code urn n) == nbsp mine elf integer brush

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
        """
        #-4???
        if n >= 1:
            return n & (n-1) == 0
        else:
            return False

class Solution(object):
    def isPowerOfTwo(self, n):
        """
        :type n: int
        :rtype: bool
        """
        #-4???
        c = 0
        while n > 0:
            if n & 1:
                c += 1
            n >>= 1
        return c == 1                    

leetcode 231. Power of Two