class Solution {
/**
* @param x: An integer
* @return: The sqrt of x
*/
public int sqrt(int x) {
// write your code here
if (x == 0 || x == 1) {
return x;
}
int start = 1;
int end = x;
while (start + 1 < end) {
int mid = start + (end - start) / 2;
int q = x / mid;
if (q == mid) {
start = mid;
} else if (q < mid) {
end = mid;
} else {
start = mid;
}
}
if ( end * end > 0 && end * end <= x ) { // x=2147483647 --> integer overflow T_T
return end;
} else {
return start;
}
}
}
T_T 溢出的那个edge case烦死了!擦!
没有评论:
发表评论