[LeetCode] Sqrt(int x)解题报告之无限逼近(牛顿法)
Implement int sqrt(int x)
.
Compute and return the square root of x.
求一个数的平方根。这道题的难点在于虽然接口的参数给的是int,但实际上如果直接用二分法,定义变量为int的时候,会提示超时(Time Limit Exceeded),如下图所示:
这个问题困扰了我很久,后来我上网一搜,才发现要用unsign long long......
其实换个思路看,在编程的时候我会发现这样一个问题,如果输入的不是平方数应该返回什么,比如输入50,那么程序应该要返回7,因为7的平方是离50最近的,于是乎,想到了无限逼近的思想。但是计算机无法处理无限的运算,如果我们取一个精度,给计算机一个计算次数的上界,那么程序就可以运行了。
逼近思想其实也就是算法里常见的迭代法,通过有限次的迭代,通过精度的判断,不断逼近正确答案。
迭代法值得注意的有两点:
1、精度;
2、迭代公式。
这道题要求sqrt(x),假设n=sqrt(x),令f(x)=x-n^2,那么这道题就简化为了求f(x)的零点。从而不难想到用牛顿法(Newton's method)解决问题。
--------------以下为引用部分-----------------------
简单推导
假设f(x)是关于X的函数:
求出f(x)的一阶导,即斜率:
简化等式得到:
然后利用得到的最终式进行迭代运算直至求到一个比较精确的满意值,为什么可以用迭代法呢?理由是中值定理(Intermediate Value Theorem):
如果f函数在闭区间[a,b]内连续,必存在一点x使得f(x) = c,c是函数f在闭区间[a,b]内的一点
我们先猜测一X初始值,例如1,当然地球人都知道除了1本身之外任何数的平方根都不会是1。然后代入初始值,通过迭代运算不断推进,逐步靠近精确值,直到得到我们主观认为比较满意的值为止。例如要求768的平方根,因为252 = 625,而302 = 900,我们可先代入一猜测值26,然后迭代运算,得到较精确值:27.7128。
回到我们最开始的那个”莫名其妙”的公式,我们要求的是N的平方根,令x2 = n,假设一关于X的函数f(x)为:
f(X) = X2 - n
求f(X)的一阶导为:
f'(X) = 2X
代入前面求到的最终式中:
Xk+1 = Xk - (Xk2 - n)/2Xk
化简即得到我们最初提到的那个求平方根的神奇公式了:
---------------------------------------------------------------
对本题,我取的精度为0.000001
代码如下:
class Solution {
public:int sqrt(int x) {double n,tmp;double d=0.0;if(x==0)return 0;if(x==1)return 1;else{n=(double)x;while(fabs(n-d)>0.000001){tmp=n;n=0.5*(tmp+(double)x/tmp);d=tmp;}return d;}}
};
运行结果示例:
[LeetCode] Sqrt(int x)解题报告之无限逼近(牛顿法)相关推荐
- 【LeetCode】3Sum Closest 解题报告
[题目] Given an array S of n integers, find three integers in S such that the sum is closest to a give ...
- LeetCode Maximum Product Subarray 解题报告
LeetCode 新题又更新了.求:最大子数组乘积. https://oj.leetcode.com/problems/maximum-product-subarray/ 题目分析:求一个数组,连续子 ...
- 【LeetCode】77. Combinations 解题报告(Python C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...
- [leetcode] 28. Implement strStr() 解题报告
题目链接:https://leetcode.com/problems/implement-strstr/ Implement strStr(). Returns the index of the fi ...
- LeetCode 488 Zuma Game 解题报告
原文链接: http://hankerzheng.com/blog/Leetcode-Zuma-Game- Problem Description LeetCode 488 Zuma Game Thi ...
- 【LeetCode】Palindrome Partitioning 解题报告
[题目] Given a string s, partition s such that every substring of the partition is a palindrome. Retur ...
- LeetCode 705 Design HashSet 解题报告
题目要求 Design a HashSet without using any built-in hash table libraries. To be specific, your design s ...
- LeetCode: First Missing Positive 解题报告
Q: Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2 ...
- [Leetcode] 625. Minimum Factorization 解题报告
题目: Given a positive integer a, find the smallest positive integer b whose multiplication of each di ...
最新文章
- pysvn安装及常用方法
- java定时器报错,定时器设置报错
- 架构日趋复杂的今天,如何重构数据库和应用边界?
- 《少年的你》惊现魅族手机,“心酸”回应:纯属意外 小破厂没钱植入
- Scala初体验之:Map
- vscode更换字体
- React——插件集合
- sql分组排序取top n
- 计算机丢失cxcore100.dll,cxcore100.dll
- 定了!自考还没有报名的每人补贴8000元!政策扶持,名额有限,速看!!!!...
- win11系统输入法增加小鹤双排
- 2018年杭州马拉松感受
- Linux内核Netfilter框架与实现
- svg怎么转为jpg格式?
- 机器人开发--雷达lidar
- 内部温度传感器和光敏传感器实验
- Dart-Aqueduct框架开发(七)
- 再介绍一家美国华人创立的超级独角兽
- 【VTK+有限元后处理】可视化结果云图
- BOJINGnet——WiFi内窥镜APP下载方法 WiFi图传内窥镜怎么使用教程