题目描述
Implementint sqrt(int x).
Compute and return the square root of x.
public class Solution {
public int sqrt(int x) {
long start = 1;
long end = x;
while(start + 1 < end){
long mid = start + (end - start) / 2;
if(mid * mid <= x){
start = mid;
}else{
end = mid;
}
}
if(end * end <= x){
return (int)end;
}
return (int)start;
}
}