Description:

Implement int sqrt(int x).

Compute and return the square root of x.

好好学习数学还是非常有用的,牛顿迭代法求解。

计算x2 = n的解,令f(x)=x2-n,相当于求解f(x)=0的解,如左图所示。

首先取x0,如果x0不是解,做一个经过(x0,f(x0))这个点的切线,与x轴的交点为x1

同样的道理,如果x1不是解,做一个经过(x1,f(x1))这个点的切线,与x轴的交点为x2

以此类推。

以这样的方式得到的xi会无限趋近于f(x)=0的解。

判断xi是否是f(x)=0的解有两种方法:

一是直接计算f(xi)的值判断是否为0,二是判断前后两个解xi和xi-1是否无限接近。

public class Solution {
public int mySqrt(int x) {
if (x ==0)
return 0;
double pre;
double cur = 1;
do
{
pre = cur;
cur = x / (2 * pre) + pre / 2.0;
} while (Math.abs(cur - pre) > 0.00001);
return (int) cur; }
}

LeetCode——Sqrt(x)的更多相关文章

  1. [LeetCode] Sqrt(x) 求平方根

    Implement int sqrt(int x). Compute and return the square root of x. 这道题要求我们求平方根,我们能想到的方法就是算一个候选值的平方, ...

  2. [leetcode]Sqrt(x) @ Python

    原题地址:https://oj.leetcode.com/problems/sqrtx/ 题意: Implement int sqrt(int x). Compute and return the s ...

  3. Leetcode Sqrt(x)

    参考Babylonian method  (x0  越接近S的平方根越好) class Solution { public: int sqrt(double x) { ) ; , tolerance ...

  4. LeetCode: Sqrt

    Title: Implement int sqrt(int x). Compute and return the square root of x. 思路:这个平方根肯定是在[1,x]之间,所以在这个 ...

  5. leetcode—sqrt

    1.题目描述   Implement int sqrt(int x).   Compute and return the square root of x. 2.解法分析 很明显,用二分搜索可解,但是 ...

  6. LeetCode:Sqrt(x) 解题报告

    Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. SOLUTION 1: 参见:二分法总结,以及模 ...

  7. [Leetcode] sqrt 开根号

    Implementint sqrt(int x). Compute and return the square root of x. 题意:求根号下x 的值 思路:使用二分搜索.先定义x平方根的取值区 ...

  8. [LeetCode] Sqrt(x) 二分搜索

    Implement int sqrt(int x). Compute and return the square root of x. Hide Tags Math Binary Search     ...

  9. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

随机推荐

  1. Android Animation动画效果简介

    AlphaAnimation 淡入淡出动画  <alpha>A fade-in or fade-out animation. Represents an AlphaAnimation. a ...

  2. es 配置文件

    [root@es02 config]# egrep -v "^(#|$)" elasticsearch.yml cluster.name: v5-applicationnode.n ...

  3. ansible 变量传递到include

    Task Include Files And Encouraging Reuse 假设您想在play或playbook中重复使用任务列表. 您可以使用include文件来执行此操作. 使用includ ...

  4. iOS开发小技巧--textField成为密码框,view加载完后textField获取焦点

    文本框安全输入:Secure Text Entry(安全文本输入) view加载完后textField获取焦点的正确做法

  5. DataGridView使用技巧十二:DataGridView Error图标表示的设定

    为了提醒用户注意,DataGridView可以使用Error图标来突出显示. Error图标可以在单元格和行头内表示,但不能在列头上显示. 1.ErrorText属性 当设定单元格/行的ErrorTe ...

  6. ubuntu 12.04 rails server 时候报错 execjs

    新的应用创建好了,使用rails server启动看看,oops!原来是没有javascript运行环境. 1 2 $ rails server  /usr/local/lib/ruby/gems/1 ...

  7. CI循环数组问题

    当我们在Controll中把数据传递到view中如: $data['cates_data']=$this->Category_Model->byid_data($id); #调用模型层查询 ...

  8. 判断asp.net中session过期的方法

    判断asp.net中session过期的方法 转载自:http://www.cnblogs.com/xilipu31/archive/2013/04/12/3016830.html 方法一:最麻烦也是 ...

  9. Linux中vi的使用

    首先,如果vi中出现了方向键变成ABCD的情况,需要卸载默认的vim-common,再安装vim. sudo apt-get remove vim-common sudo apt-get instal ...

  10. 在安装ZooKeeper之前,请确保你的系统是在以下任一操作系统上运行

    在安装ZooKeeper之前,请确保你的系统是在以下任一操作系统上运行: 任意Linux OS - 支持开发和部署.适合演示应用程序. Windows OS - 仅支持开发. Mac OS - 仅支持 ...