12345678910111213141516171819202122232425 |
- def mySqrt(x: int) -> int:
- # if x == 0:
- # return 0
- # if x == 1:
- # return 1
- # i = 0
- # while i <= x//2:
- # if (i+1) * (i+1) > x and i * i <= x:
- # return i
- # elif (i+1) * (i+1) == x:
- # return i + 1
- # else:
- # i += 1
- l, r, ans = 0, x, -1
- while l <= r:
- mid = l + (r - l) // 2
- if mid * mid <= x:
- ans = mid
- l = mid + 1
- else:
- r = mid - 1
- return ans
- print(mySqrt(10))
|