暴力搜索
class Solution: def mySqrt(self, x: int) -> int: if x>=100 and x<10000: i=10 elif x>=10000 and x<1000000: i=100 elif x>=1000000: i=1000 else: i=0 flag=True while flag: if i*i==x: return i elif i*i<x and (i+1)*(i+1)>x: return i else: i+=1
二分法
class Solution: def mySqrt(self, x: int) -> int: if x == 0: return 0 left = 1 right = x res = 0 while left <= right: mid = left + (right - left) // 2 if mid*mid== x: return mid elif mid * mid < x: left = mid + 1 res = mid else: right = mid - 1 return res
标签:return,int,self,Solution,leedcode,平方根,else From: https://www.cnblogs.com/yyyjw/p/17986497