题意:
不使用sort(x),实现求x的平方根。
思路:
二分搜索x……
代码:
class Solution {
public:
int mySqrt(int x) {
if (x == 0 || x == 1) {
return x;
}
int ans = 1;
int l = 1, r = x / 2;
while (l <= r) {
int mid = (l + r) >> 1;
if (x / mid >= mid) {
ans = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
return ans;
}
};