<aside> 👨💻 二分算法的右边界问题: 二分查找系列
</aside>
class Solution {
public int mySqrt(int x) {
// 尝试使用二分法
// 当 a^2 < x, (a+1)^2 > x 时,即确定x的算术平方根为a
// 确定二分区间
int a = 1;
int b = x;
int ans = -1;
while (a <= b) {
int t = a + (b - a) / 2;
if ((long) t * t <= x) {
ans = t;
a = t + 1;
} else {
b = t - 1;
}
}
return ans;
}
}
// TODO
// TODO