69. Sqrt(x)
Knowledge point:
1. binary search: low , high mid
while(low<=high) mid=(high-low)/2+low
2 Math calculation :
long mid
3 sqrt: mid*mid<high
public int mySqrt(int x) {
if(x<=0) return 0;
int low=1;
int high=x;
while(low<=high){
long mid=(high-low)/2+low;
if(mid*mid==x)
return (int)mid;
else if(mid*mid<x)
low=(int)mid+1;
else high=(int)mid-1;
}
if(high*high<x)
return high;
else
return low;
}