原题网址:https://leetcode.com/problems/sqrtx/
Implement int sqrt(int x)
.
Compute and return the square root of x.
方法一:二分法。
public class Solution {
public int mySqrt(int x) {
int i=0, j=x;
while (i<=j) {
int m = (i+j)/2;
long m2 = (long)m*m;
if (m2==(long)x) return m;
if (m2>x) {
j = m - 1;
} else {
i = m + 1;
}
}
return i-1;
}
}
方法二:牛顿迭代法。
public class Solution {
public int mySqrt(int x) {
if (x == 0) return 0;
double y = Math.max(1, x/2);
while (true) {
double ny = (((double)y*y+x)/2/y);
if (Math.abs(y-ny) <= 0.01) return (int)ny;
y=ny;
}
}
}