2023每日刷题(二十七)
Leetcode—69.x的平方根
直接法实现代码
int mySqrt(int x) {long long i = 0;while(i * i < x) {i++;}if(i * i > x) {return i - 1;}return i;
}
运行结果
二分法实现代码
int mySqrt(int x) {long long left = 0, right = (long long)x + 10;long long mid = left + (right - left) / 2;while(left < right) {mid = left + (right - left) / 2;if(mid * mid < x) {if((mid + 1) * (mid + 1) > x) {return mid;}left = mid + 1;} else if(mid * mid > x) {right = mid;} else {return mid;}}return mid;
}
运行结果
之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!