题目:

Implement int sqrt(int x).

Compute and return the square root of x.

链接:   http://leetcode.com/problems/sqrtx/

题解:

求平方根。

二分法, Time Complexity - O(logn), Space Complexity - O(1)

public class Solution {
public int mySqrt(int x) {
if(x <= 1)
return x;
int lo = 0, hi = x; while(lo <= hi) {
int mid = lo + (hi - lo) / 2;
if(mid < x / mid)
lo = mid + 1;
else if (mid > x / mid)
hi = mid - 1;
else
return mid;
} return hi;
}
}

牛顿法

public class Solution {
public int sqrt(int x) {
if (x == 0) return 0;
double lastY = 0;
double y = 1; while (y != lastY) {
lastY = y;
y = (y + x / y) / 2;
} return (int)(y);
}
}

Bit Manipulation

public class Solution {
public int mySqrt(int x) {
long ans = 0;
long bit = 1l << 16; while(bit > 0) {
ans |= bit;
if (ans * ans > x) {
ans ^= bit;
}
bit >>= 1;
} return (int)ans;
}
}

Follow up - 求实数的平方根。 设置一个ε,然后根据差值计算

二刷:

可以用二分法或者牛顿法。

Java:

二分法:

二分法写得比较古怪,有点像背出来的, 为了避免Integer.MAX_VALUE用了 x / mid。mid不会为0,所以可以放心大胆使用。 最后返回结果时要返回的是hi, 这是 lo ^2会正好比x大。

Time Complexity - O(logn), Space Complexity - O(1)

public class Solution {
public int mySqrt(int x) {
if (x <= 1) {
return x;
}
int lo = 0, hi = x;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (mid == x / mid) {
return mid;
} else if (mid < x / mid) {
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return hi;
}
}

牛顿法:

牛顿法也可以用来处理double的情况,只要把条件改为y - lastY > epsilon就可以了。一开始设x2 = a,则转化为方程y = x2 - a, 接下来右xn+1 = xn - f(x) / f'(x)。    新的y (切线逼近)等于 xn+1 = xn -  ( x2- a )  / (y的导数 = 2x),所以简化一下就等于xn+1 = (xn + a / xn) / 2,用切线进行不断逼近。

Time Complexity - O(logn), Space Complexity - O(1)

public class Solution {
public int mySqrt(int x) {
if (x <= 1) {
return x;
}
double lastY = x / 2;
double y = (lastY + x / lastY) / 2;
while (y - lastY != 0) {
lastY = y;
y = (lastY + x / lastY) / 2;
}
return (int)y;
}
}

三刷:

Java:

二分法:

public class Solution {
public int mySqrt(int x) {
if (x <= 1) return x;
int lo = 0, hi = x;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (mid == x / mid) return mid;
else if (mid < x / mid) lo = mid + 1;
else hi = mid - 1;
}
return hi;
}
}

牛顿法:

再次详述一下牛顿法。

牛顿法主要是使用切线来不断逼近方程根的方法。

  1. 首先我们有x2 = a,移动一下我们得到方程x2 - a = 0。
  2. 我们假设函数 f(x) = x2 - a, 则这个方程的切线 f'(x) = 2x,也就是切线的斜率为2
  3. 利用直线的两点式方程我们可以得到 xn+1 - x= ( 0 - f(xn) ) / f'(xn),  这里新的逼近点为(xn+1 , 0),即切线与x轴的焦点, 旧的逼近点为(xn  , f(x))。
  4. 这里我们做一下变量代换, f'(x) = 2x,  f(x) = x2 - a
  5. 我们可以得到  xn+1 - x= (a - xn2) / 2 * x, 简化一下就变成了  xn+1 = (xn + a / xn) / 2
  6. 在 xn+1 - x的差大于一个误差常数Epsilon的时候,我们就可以用while循环来不断使用切线迭代逼近方程x2 - a = 0的根, 最后就可以成功求得一个在Epsilon误差内的,方程的解。
public class Solution {
public int mySqrt(int x) {
if (x <= 1) {
return x;
}
double lastY = 1;
double y = (lastY + x / lastY) / 2;
while (y - lastY != 0) {
lastY = y;
y = (lastY + x / lastY) / 2;
}
return (int)y;
}
}

4/4/2016: 从wentao处又学习了很多东西。他是数学PHD大牛,推理和论证功力简直一级棒。

Reference:

http://www.matrix67.com/blog/archives/361

https://en.wiki2.org/wiki/Methods_of_computing_square_roots

http://www.math.harvard.edu/library/sternberg/slides/lec1.pdf

http://www.cnblogs.com/bigrabbit/archive/2012/09/25/2702174.html

69. Sqrt(x)的更多相关文章

  1. C++版 - Leetcode 69. Sqrt(x) 解题报告【C库函数sqrt(x)模拟-求平方根】

    69. Sqrt(x) Total Accepted: 93296 Total Submissions: 368340 Difficulty: Medium 提交网址: https://leetcod ...

  2. Leetcode 69. Sqrt(x)及其扩展(有/无精度、二分法、牛顿法)详解

    Leetcode 69. Sqrt(x) Easy https://leetcode.com/problems/sqrtx/ Implement int sqrt(int x). Compute an ...

  3. 69. Sqrt(x) - LeetCode

    Question 69. Sqrt(x) Solution 题目大意: 求一个数的平方根 思路: 二分查找 Python实现: def sqrt(x): l = 0 r = x + 1 while l ...

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

    Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a no ...

  5. Leetcode 69. Sqrt(x)

    Implement int sqrt(int x). 思路: Binary Search class Solution(object): def mySqrt(self, x): "&quo ...

  6. 【一天一道LeetCode】#69. Sqrt(x)

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Impleme ...

  7. (二分查找 拓展) leetcode 69. Sqrt(x)

    Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a no ...

  8. [LeetCode] 69. Sqrt(x)_Easy tag: Binary Search

    Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a no ...

  9. 69. Sqrt(x)(二分查找)

    Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a no ...

随机推荐

  1. 1100. Mars Numbers (20)

    People on Mars count their numbers with base 13: Zero on Earth is called "tret" on Mars. T ...

  2. 用Redis bitmap统计活跃用户、留存

    Spool的开发者博客,描述了Spool利用Redis的bitmaps相关的操作,进行网站活跃用户统计工作. 原文:http://blog.getspool.com/2011/11/29/fast-e ...

  3. postgreSQL数据库(索引、视图)

    索引的含义与特点 索引是一个单独的.存储在磁盘上的数据库结构,它们包含对数据所有记录的引用指针,postgresql列类型都可以被索引,对相关列索引是提高查询操作效率的最佳途径.例如,查询select ...

  4. Swift学习:闭包(Closures)

    /* 闭包(Closures)* 闭包是自包含的功能代码块,可以在代码中使用或者用来作为参数传值.* 在Swift中的闭包与C.OC中的blocks和其它编程语言(如Python)中的lambdas类 ...

  5. EntityFramework中的datetime2异常的解决

    (转)   最近使用.net的Entity Framework构建网站数据层,给一个实体的DATETIME类型的属性赋值时 突然莫名奇妙显示有一个类型不匹配的异常如下: System.Data.Sql ...

  6. JAVA类与对象(一)----基础概念理解

    面向对象基本概念 面向对象是一种新兴的程序设计方法,或者说是一种新的程序设计规范,其基本思想是使用对象.类.继承.封装.消息等基本概念来进行程序设计.它是从现实世界客观存在的事物(即对象)出发来构造软 ...

  7. jquery-2.0.3.js和jquery-2.0.3.min.js的区别

    两个文件的作用是完全一样的. jquery-2.0.3.js里的代码是没有进行处理的原代码,适合于人们阅读与研究. jquery-2.0.3.min.js里的代码进行过特殊的处理, 如变量的名称基本都 ...

  8. svn:Repository UUID 'XXX' doesn't match expected UUID 'YYY'

    About a month ago, CodePlex have upgraded their TFS servers to to TFS 2010. While this transition wa ...

  9. 2208: [Jsoi2010]连通数 - BZOJ

    Description Input 输入数据第一行是图顶点的数量,一个正整数N. 接下来N行,每行N个字符.第i行第j列的1表示顶点i到j有边,0则表示无边. Output 输出一行一个整数,表示该图 ...

  10. BZOJ 3436: 小K的农场 差分约束

    题目链接: http://www.lydsy.com/JudgeOnline/problem.php?id=3436 题解: 裸的差分约束: 1.a>=b+c  ->  b<=a-c ...