您的位置:首页 > 其它

leetcode69: Sqrt(x)

2017-07-02 20:56 477 查看
要求:

Implement 
int sqrt(int x)
.

Compute and return the square root of x.

注意:使用牛顿梯度法计算平方根,看百度百科或者维基百科

public int mySqrt(int x) {
if (x== 0)
return 0;
double sol = 1;
double res = 0;
while(sol - res!=0) {
res = sol;
sol = 0.5*(sol+x/sol);
}
return (int) sol;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: