c++二分法求平方根
class Solution {
public:
/**
*
* @param x int整型
* @return int整型
*/
int sqrt(int x) {
// write code here
//二分法
int left=1,right=x;
if(x1||x0)
{
return x;
}
while(left<=right)
{
int mid=(left+right)/2;
if(mid<=x/mid&&(mid+1)>x/(mid+1))//4
return mid;
if(mid>x/mid)//6
{
right=mid-1;
}
else
{
left=mid+1;
}
}
return 0;
08-18
183
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
09-12
1298
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)