题目
class Solution {
public:
int mySqrt(int x) {
long long int y=x;
int l=0;
int r=(x==1?1:x/2);
while(l<=r)
{
long long int mid=(l+r)/2;
if(mid*mid<y)
{
l=mid+1;
}
else if(mid*mid >y)
{
r=mid-1;
}
else
{
return mid;
}
}
return r;
}
};
题目
class Solution {
public:
int mySqrt(int x) {
long long int y=x;
int l=0;
int r=(x==1?1:x/2);
while(l<=r)
{
long long int mid=(l+r)/2;
if(mid*mid<y)
{
l=mid+1;
}
else if(mid*mid >y)
{
r=mid-1;
}
else
{
return mid;
}
}
return r;
}
};