Implement int sqrt(int x)
.
Compute and return the square root of x.
class Solution {
public:
//二分查找
int sqrt(int x) {
long long left = 0, right = x;
while (left <= right) {
long long mid = (left + right) / 2;
if (mid*mid == x)
return mid;
else if (mid*mid < x)
left = mid + 1;
else
right = mid - 1;
}
return right;
}
};