69. Sqrt(x)
题目:
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;
}
}
牛顿法:
再次详述一下牛顿法。
牛顿法主要是使用切线来不断逼近方程根的方法。
- 首先我们有x2 = a,移动一下我们得到方程x2 - a = 0。
- 我们假设函数 f(x) = x2 - a, 则这个方程的切线 f'(x) = 2x,也就是切线的斜率为2
- 利用直线的两点式方程我们可以得到 xn+1 - xn = ( 0 - f(xn) ) / f'(xn), 这里新的逼近点为(xn+1 , 0),即切线与x轴的焦点, 旧的逼近点为(xn , f(xn ))。
- 这里我们做一下变量代换, f'(x) = 2x, f(x) = x2 - a
- 我们可以得到 xn+1 - xn = (a - xn2) / 2 * xn , 简化一下就变成了 xn+1 = (xn + a / xn) / 2
- 在 xn+1 - xn 的差大于一个误差常数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)的更多相关文章
- C++版 - Leetcode 69. Sqrt(x) 解题报告【C库函数sqrt(x)模拟-求平方根】
69. Sqrt(x) Total Accepted: 93296 Total Submissions: 368340 Difficulty: Medium 提交网址: https://leetcod ...
- Leetcode 69. Sqrt(x)及其扩展(有/无精度、二分法、牛顿法)详解
Leetcode 69. Sqrt(x) Easy https://leetcode.com/problems/sqrtx/ Implement int sqrt(int x). Compute an ...
- 69. Sqrt(x) - LeetCode
Question 69. Sqrt(x) Solution 题目大意: 求一个数的平方根 思路: 二分查找 Python实现: def sqrt(x): l = 0 r = x + 1 while l ...
- [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 ...
- Leetcode 69. Sqrt(x)
Implement int sqrt(int x). 思路: Binary Search class Solution(object): def mySqrt(self, x): "&quo ...
- 【一天一道LeetCode】#69. Sqrt(x)
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Impleme ...
- (二分查找 拓展) 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 ...
- [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 ...
- 69. Sqrt(x)(二分查找)
Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a no ...
随机推荐
- Windows Server 2008 HPC 版本介绍以及的Pack
最近接触了下 这个比较少见的 Windows Server版本 Windows Server 2008 HPC 微软官方的介绍 http://www.microsoft.com/china/hpc/ ...
- H5不能少的功能-滑动分页
// 滑动分页 $(window).scroll(function() { var mayLoadContent = $(window).scrollTop() & ...
- 十一、 BOOL类型、分支结构和关系运算符
BOOL类型:表示非真即假.只有两个值:YES和NO,而二进制只识别二进制数,所以,将YES替换为“1”,NO替换为“0” BOOL数据类型占一字节的空间内存 BOOL数据类型输出为:%lu:输入为: ...
- SNV ConnerStore使用说明
1. 上传自己新建的文件新建的类文件 后面的 会有A标示要先 Add To Working copy 再点击提交 2. 上传第三方库时 默认SVN是忽略.a文件的要找到.a文件把他设置成不是忽略的通 ...
- 浅析JVM内存结构和6大区域(转)
其实对于我们一般理解的计算机内存,它算是CPU与计算机打交道最频繁的区域,所有数据都是先经过硬盘至内存,然后由CPU再从内存中获取数据进行处理,又将数据保存到内存,通过分页或分片技术将内存中的数据再f ...
- Eclipse 3.7(代号Indigo) 中文字体太小解决办法(转)
升级到3.7Eclipse最直观的反映就是,中文怎么那么小啊---- 相当不方便. 其实这是Eclipse的默认字体换了,以前的一直是Courier New,现在修改字体也找不到了,算了不找了. 这次 ...
- PHP Cookie处理函数
(o゜▽゜)o☆[BINGO!] ok,我们先看看cookie是什么东东? cookie是服务器留在客户端的用于识别用户或者存储一些数据的小文件(注意,session存储在服务器端,这是两者的区别之一 ...
- Sql例子Sp_ExecuteSql 带参数
Declare @i int, @projectCount int ) --参数 ) ) ) ) ) ) --循环变量起始 --得到所有的项目 select @projectCount = count ...
- 解决Maven默认仓库没有的jar下载(二)
前言: 在 “解决Maven不能下载“oracle.aspectjweaver.com.springsource.net.sf.cglib”jar(http://www.cnblogs.com/wql ...
- 【技术贴】解决Eclipse中SVN图标不显示
在用Eclipse做开发的时候,用到了svn版本控制器,这天当我打开Eclipse的时候,发现项目里面的所有文件前的版本号以及状态图标都不显示了,即所有的svn图标不显示了,这是怎么回事,关掉Ecli ...