95%+ python solution


  • 0
    A

    Basic binary search method in python:

    class Solution(object):
        def mySqrt(self, x):
            """
            :type x: int
            :rtype: int
            """
            if x == 0:
                return 0
            left = 1
            right = x
            while left <= right:
                mid = (left + right) >> 1
                if mid * mid > x:
                    right = mid
                elif mid * mid < x:
                    if mid == left:
                        return mid
                    left = mid
                else:
                    return mid
            return None
    

Log in to reply
 

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