模板code
考虑mid vs x
如果mid <= x,答案在[mid, r]之间;
如果mid*mid > x, 答案在[l, mid - 1]之间;
第二种分法,mid = (l + r + 1)//2
class Solution:
def mySqrt(self, x: int) -> int:
if x == 0 or x == 1 : return x
l, r = 0, x
while l < r:
mid = (l + r + 1)//2 #//整数除法
if mid <= x//mid:
l = mid
else:
r = mid - 1
return l