class Solution:
def mySqrt(self, x: int) -> int:
# 顺序查找,寻找一个数m m的平方比该<=x且(m+1)的平方>x 理论可行但超时
if x == 0 or x == 1:
return x
for i in range(1,x):
if i**2<=x and(i+1)**2>x:
return i
思路:
用二分法拟合,然后实际上就是找它的右坐标
# 这个二分之前做过 但是这次没了思路。。
class Solution:
def mySqrt(self, x: int) -> int:
if x == 0 or x== 1:
return x
left,right = 0,x-1
while(left<=right):
middle = (left+right) // 2
if middle ** 2 > x:
right = middle - 1
else:
left = middle +1
return right