class Solution {
public:
int checksqrt(int low,int high,int c){//二分法寻找元素x的平方根
int mid=low+(high-low)/2;
if (low<=high)
{
if(c/mid==mid)
return mid;
else if(c/mid>mid)
return checksqrt(mid+1,high,c);
else
return checksqrt(low,mid-1,c);
}
return high;
}
int mySqrt(int x) {
if(x==0)
return 0;
int c=checksqrt(1,x,x);
return c;
}
};
代码随想录学习笔记(x的平方根)
最新推荐文章于 2024-09-06 10:38:06 发布