Two simple python solution


  • 0
    P
    # divide-by-2
    class Solution(object):
        def isPowerOfTwo(self, n):
            if n <= 0:
                return False
            while n % 2 == 0:
                n /= 2   
            return n == 1
    
    # power of 2 should only have 1 bit that is on
    class Solution(object):
        def isPowerOfTwo(self, n):
            return False if n <= 0 else bin(n).count('1') == 1

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.