
T69:
Implement int sqrt(int x).
Compute and return the square root of x.
x is guaranteed to be a non-negative integer.
public class Solution{
public int Sqrt(int x){
int low=1,high=x;
while(low<=high){
long mid=(high-low)/2+low;
if(mid*mid==x){
return (int)mid;
}else if(mid*mid<x){
high=(int)mid-1;
}else{
low=(int)mid+1;
}
}
if (high * high < x) {
return (int) high;
} else {
return (int) low;
}
}
}




近期评论