采用二分的策略:
class Solution {
public:
int sqrt(int x) {
if(x <= 0)
return 0;
long long low = 1, high = x, mid;
while(low <= high){
mid = (low + high) >> 1;
if(mid * mid > x)
high = mid - 1;
else if(mid * mid == x)
return mid;
else if(mid * mid < x)
low = mid + 1;
}
return low - 1;
}
};