原题网址:
https://leetcode.com/problems/sqrtx/#/description
Implement int sqrt(int x)
. Compute and return the square root of x.
题解:
求正整数
x
的开方,即找到
最简单的想法就是使 y 从零开始计算,逐次加1,直到满足要求为止,但是这样需要的时间非常久,在LeetCode中会报错超出时间。
这里我们用一个最优化的方法——牛顿法。
牛顿法首先将非线性函数
取线性部分得到:
x0
是一个初始的自己设置的值,通过一次计算我们可以得到一个近似解
x
,但这个解并不是真实的解。我们令
下面这个图取自wiki,非常直观的展示了牛顿法迭代逼近最优解的过程:
在这道题中我们构造的函数
设初始的 y0=1 。
附上代码:
class Solution {
public:
int mySqrt(int x) {
if (x==0) return 0;
float y = 1, y1;
int output;
while(1)
{
y1 = (x + y*y) / (2*y);
if(abs(y1-y) < 0.1) break;
y = y1;
}
output = int(y);
while(output*output > x)
{
output -= 1;
}
return output;
}
};
转载请附上本文链接:
http://blog.csdn.net/marywbrown/article/details/69060150