class Solution {
/**
* @param x: An integer
* @return: The sqrt of x
*/
public int sqrt(int x) {
// 2015-09-06 用int会溢出
long start = 0;
long end = x / 2 + 1;
while (start <= end) {
long mid = start + (end - start) / 2;
if ((mid * mid) > x) {
end = mid - 1;
} else if ((mid + 1) * (mid + 1) <= x) {
start = mid + 1;
} else {
return (int)mid;
}
}
return 0;
}
}
[刷题]Sqrt(x)
最新推荐文章于 2021-10-06 22:43:49 发布