LeetCode69 x的平方根 (二分)

LeetCode69 x的平方根

class Solution(object):
    def mySqrt(self, x):
        """
        :type x: int
        :rtype: int
        """
        l, r, ans = 0, x, -1
        
        while l <= r:
            mid = (l + r) // 2
            if mid * mid <= x:
                ans = mid
                l = mid + 1
            else:
                r = mid - 1
        return ans

posted on 2022-06-29 11:00  solvit  阅读(16)  评论(0编辑  收藏  举报

导航