[LeetCode] 69. Sqrt(x)
2016-08-01 19:14
609 查看
思路:
牛顿迭代法. 假设f(X) = 0在X0处有一个根, 那么Xn+1 = Xn - f(Xn) / f’(Xn). 所以对于f(X) = X^2 - a. Xn+1 = Xn - (Xn^2 - a) / (2Xn)
牛顿迭代法. 假设f(X) = 0在X0处有一个根, 那么Xn+1 = Xn - f(Xn) / f’(Xn). 所以对于f(X) = X^2 - a. Xn+1 = Xn - (Xn^2 - a) / (2Xn)
int mySqrt(int x) { if (! x) return x; double epsilon = 0.001; double prev = 1, cur = 1; do { prev = cur; cur = (prev + x / prev) * 1.0 / 2; } while (fabs(prev - cur) > epsilon); return cur; }
相关文章推荐
- LeetCode-69-Sqrt(x)-E
- [leetcode] 69.Sqrt(x)
- LeetCode 69 - Sqrt(x)
- 【LeetCode】69. Sqrt(x)
- 【LeetCode】69. Sqrt(x)解法及注释
- LeetCode 69. Sqrt(x)
- LeetCode:69. Sqrt(x)
- leetcode 69. Sqrt(x)
- 【leetcode】69. Sqrt(x)(Python & C++)
- LeetCode-69.Sqrt(x)
- Sqrt(x) - LeetCode 69
- leetcode - 69.Sqrt(x)
- [leetcode] 69 Sqrt(x)
- LeetCode - 69. Sqrt(x)
- LeetCode 69. Sqrt(x)
- 2017.10.19 LeetCode 二分 -> 69. Sqrt(x) -> 410. Split Array Largest Sum
- 【leetcode】69. Sqrt(x)【M】【74】
- [LeetCode] 69. Sqrt(x) 求平方根
- leetcode 69. Sqrt(x)
- LeetCode 69. Sqrt(x)(开平方根)