Implement int sqrt(int x)
.
Compute and return the square root of x.
这里使用牛顿迭代法,详情可参考点击打开链接
public class Solution {
public int mySqrt(int x) {
long n = x;
while(n*n>x)
{
n = (n+(x/n))/2;
}
return (int)n;
}
}