public class Solution {
public int mySqrt(int x) {
if(x==0) return 0;
if(x<0) return x;
int start = 1, end = x;
while(true){
int mid = start + (end-start)/ 2;
if(mid > x/mid) {
end = mid-1;
}
else{
if(mid+1 > x/(mid+1))
return mid;
start = mid+1;
}
}
}
}
public class Solution {
public int mySqrt(int x) {
int i = 0;
int j = x / 2 + 1;
while (i <= j) {
int mid = i + (j - i) / 2;
long product = (long)mid * mid;
if (product == x) return mid;
else if (product > x) {
j = mid - 1;
} else {
i = mid + 1;
}
}
return j;
}
}