Implement int sqrt(int x)
.
Compute and return the square root of x.
//这里mid要用long long,否则越界
class Solution {
public:
int sqrt(int x) {
// Note: The Solution object is instantiated only once and is reused by each test case.
if(x <= 0) return 0;
int low = 0, high = x;
while(low <= high){
long long mid = (low + high) /2;
if(mid * mid <= x)
low = mid + 1;
else
high = mid - 1;
}
return high;
}
};