Implement int sqrt(int x).
Compute and return the square root of x.
顺序实现会造成超时,可以对半搜索。
public int mySqrt(int x) {
long s=0;long e=x;
while(e>=s){
long mid=s+(e-s)/2;
if(x<mid*mid)
e=mid-1;
else if(x>mid*mid)
s=mid+1;
else
return (int) mid;
}
return (int)e;
}