Implement int sqrt(int x).
Compute and return the square root of x.
public class Solution {
public int mySqrt(int x) {
if(x == 0) {
return 0;
}
int left = 1;
int right = x / 2 + 1;
while(left <= right) {
int mid = (right - left) / 2 + left;
if(mid == x / mid) {
return mid;
} else if(x / mid > mid) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return right;
}
}