class Solution {
public:
int mySqrt(int x) {
unsigned long left = 0, right = x;
while (left < right) {
unsigned long middle = left + (right - left + 1) / 2;
unsigned long tmp = middle * middle;
if (x < tmp) {
right = middle - 1;
} else {
left = middle;
}
}
if (x < left * left) {
return left - 1;
}
return left;
}
};
x
is the input nonnegative integer.
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up