Two Python solution, log(n) and n complexity


  • 1
    A

    log(n) solution:

    def isPowerOfTwo(self, n):
        if n <= 0:
        	return False
    
        while n % 2 == 0:        	
        	n = n / 2
    
        return n == 1
    

    n solution:

    def isPowerOfTwo(self, n):
        if n <= 0:
        	return False
    
        if n & (n-1) == 0:
        	return True
    
        return False

  • 0
    B

    why the later one is o(n) complexity


Log in to reply
 

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