版权声明:博客文章都是作者辛苦整理的,转载请注明出处,谢谢! https://cloud.tencent.com/developer/article/1433893
文章作者:Tyan
博客:noahsnail.com | CSDN | 简书
class Solution {
public:
int mySqrt(int x) {
int left = 0;
int right = x;
while(left <= right) {
long mid = (left + right) / 2;
long square = mid * mid;
if(square == x) {
return mid;
}
if(square > x) {
right = mid - 1;
}
else {
left = mid + 1;
}
}
return right;
}
};
class Solution {
public:
int mySqrt(int x) {
long y = x;
while(y * y > x) {
y = (y + x / y) / 2;
}
return y;
}
};