class Solution {
public:
int sqrt(int x) {//二分查找,如果不能开方,返回最大的数y,使得y^2<x且(y+1)^>x
if(x<=1)
return x;
int low=1;
int high=x;
unsigned long long temp;
unsigned long long mid;//必须要无符号长整形,否则相乘时会超出int技术范围
while(low<high)
{
//if(low==high)
//return low;
mid=low+(high-low)/2;
temp=mid*mid;
if(temp==x)
return mid;
if(temp<x)
low=mid+1;
if(temp>x)
high=mid-1;
}
temp=low*low;
if(temp<=x)//这里别忘了等于的情况,易错点
return low;
else
return low-1;
}
};