Implement int sqrt(int x)
.
Compute and return the square root of x.
Java Solutions:
public class Solution {
public int sqrt(int x) {
if(x<0)
return -1;
long high=x/2+1;
long low=0;
while(low<=high){
long mid=low+(high-low)/2;
long sqr=mid*mid;
if(sqr==(long)x)
return (int)mid;
else if(sqr<(long)x)
low=mid+1;
else
high=mid-1;
}
return (int)high;
}
}