sqrt_x

  • 2022-12-14
  • 浏览 (638)

sqrt_x.py 源码

# 求x的平方根

class Solution:

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

你可能感兴趣的文章

find_peak_element

first_last_sort_array

perfect_square

0  赞