69. x 的平方根
解题思路

- 使用二分查找
- 注意 这里当计算的Mid在x的平方根附近之后,直接返回mid即可
class Solution {
public int mySqrt(int x) {
// 二分查找 查找区间 mid * mid == x
int left = 0;
int right = x/ 2 + 1;
int mid = 0;
if(x == 0){
return 0;
}
if(x == 1){
return 1;
}
while(left <= right){
mid = left + (right - left) / 2;
if(mid < x/mid){
left = mid + 1;
}
else if(mid > x / mid){
right = mid - 1;
}else if(mid <= x / mid && (mid + 1) > x / (mid + 1)){
// 当要求的mid在根附近之后 直接返回mid
return mid;
}
}
return right;
}
}





![[QT编程系列-1]:C++图形用户界面编程,QT框架快速入门培训 - 0- 总述](https://img-blog.csdnimg.cn/a1cce0d0c8554a60bf3494ca2239a0bd.png)













