// 二分查找
class Solution {
public int mySqrt(int x) {
long l=0, r=x;
while(l < r){
long m = (l+r+1) >>> 1;
if(m*m > x) r = m - 1;
else l = m; //[l,r]
}
return (int)l;
}
}
// 牛顿迭代法
// https://leetcode.cn/problems/sqrtx/solution/by-zhang-jian-qi-shi-kaxt/
class Solution {
// y=x^2 - a
public int mySqrt(int x) {
long a = x;
long res = x;
while(res*res > x){
res = (res + a/res)/2;
}
return (int)res;
}
}
x的平凡根
最新推荐文章于 2024-10-06 13:05:10 发布