My Simply Python Using Math o(logn)


  • 0
    J
    class Solution(object):
        def mySqrt(self, x):
            """
            :type x: int
            :rtype: int
            """
            import math
            if x == 0:
                return 0
                
            a = 1.0
            b = x
    
            while abs(a-b)>1:
                a = (a+b)/2
                b = x/a
    
            return int((a+b)/2)
    

Log in to reply
 

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