leetcode-x的平方根

69. x 的平方根

此题使用二分法做

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

你可能感兴趣的:(leetcode)