leetcode 69. x 的平方根

二分法实现

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

 

上一篇:[LeetCode] 69. x 的平方根


下一篇:(69)zabbix监控惠普打印机