class Solution {
public:
int sqrt(int x) {
int left = 0, right = x/2;
long long mid = 0;
while (left <= right) {
mid = (right+left)/2;
long long cur = mid*mid;
long long pre = (mid+1)*(mid+1);
if(cur==x)
return mid;
else if(x<pre&&x>cur)
return mid;
else if(cur>x)
right=mid-1;
else
left=mid+1;
}
return 1;
}
};
lintcode-x的平方根-141
最新推荐文章于 2019-04-03 15:16:00 发布