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. [Makefile]多文件的通用Makefile

    下面是一个糅合多线程和多文件的示例 emc-test.c #include <stdio.h> #include <pthread.h> #include "char ...

  2. linux_UAPI_转

    转自:Linux Kernel UAPI 问题描述 从3.5开始,Linux Kernel 里多了一个 uapi 文件夹,里面放了很多 Linux Kernel 各个模块的头文件.如果是第一次碰到,可 ...

  3. linux挂载根文件系统过程

    linux-2.6.36内核 start       arch/arm/boot/compressed/head.S arch/arm/kernel/head.S start_kernel()    ...

  4. UVALive-4670 AC自动机入门题 求出现次数最多的子串

    /** 链接:http://vjudge.net/problem/UVALive-4670 详见lrj训练指南P216 */ #include<bits/stdc++.h> using n ...

  5. hdu 1595 find the longest of the shortest(dijstra + 枚举)

    http://acm.hdu.edu.cn/showproblem.php?pid=1595 大致题意: 给一个图.让输出从中删除随意一条边后所得最短路径中最长的. . 思路: 直接枚举每条边想必是不 ...

  6. sparkr——报错

    > sc <- sparkR.init() Re-using existing Spark Context. Please stop SparkR with sparkR.stop() o ...

  7. BUILD_BUG_ON

    BUILD_BUG_ON() 在编译时调用,可以提前发现错误,这里利用了一些不常用的特性,当数组个数元素为负时会发生编译器错误,对于位域宽度而言,其为负数时也会发生编译器错误. #define BUI ...

  8. Windows下配置Apache+PHP跑Wordpress拾遗

    首先,我很少这么做,因为一旦有跑WAMP的需求,我就直接下一个wamp的安装包就可以了,市面上数不胜数,我一直用的是EasyPHP,不是说它有多好,而是很多年前第一次用后没什么问题,就一直用下来了.这 ...

  9. 对ChemDraw Professional 16.0你了解多少

    ChemDraw Professional 16.0组件为科学家提供了最新的科学智能应用程序组件,绘制化学结构图和分析生物路径图.  ChemDraw Professional 16.0 ChemDr ...

  10. mongodb查询内嵌文档

    mongodb查询内嵌文档   假设有这样一个文档: db.XXX.remove(); db.XXX.insert({"id":1, "members":[{& ...