Easy
有一个不用转换类型的方法,写mid*mid == x时换成mid == x/mid或者mid < x/mid这样,可以防止越界
class Solution {
public int mySqrt(int x) {
if (x == 0){
return 0;
}
int start = 1;
int end = x;
int mid = 0;
while (start + 1 < end){
mid = start + (end - start) / 2;
if (mid == x / mid){
return mid;
} else if (mid < x / mid){
start = mid;
} else {
end = mid;
}
}
return start;
}
}