leetcode 69. xの平方根二分法python


     ,          ,             。
     。
class Solution:
    def mySqrt(self, x: int) -> int:
        left, right = 1, x/2 + 1
        while left <= right: #          
            mid = (left + right) // 2
            if mid > x / mid: #        x ,     x      
                right = mid - 1 #       
            elif mid < x / mid: #  
                left = mid + 1
            else: #        x 
                return int(mid)
        return int(right) #          ,     
        #          ,     +1       x,           。