Very simple 64 ms Python solution


  • 1
    C

    This is my passing 64ms solution in Python without using bitwise operations. One problem I ran into with this approach is that the machine's inaccurate storage of floating-point values led to erroneous results for large values of n, so I rounded the result out to 11 decimal places.

    import math
    class Solution(object):
    def isPowerOfTwo(self, n):
        """
        :type n: int
        :rtype: bool
        """
        
        if n <= 0:
            return False
        
        calc = math.log(n)/math.log(2) % 1
        
        calc = round(calc, 11)
        
        return calc == 0.0

Log in to reply
 

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