LeetCode(69):x 的平方根
Easy!
题目描述:
实现 int sqrt(int x)
函数。
计算并返回 x 的平方根,其中 x 是非负整数。
由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去。
示例 1:
输入: 4
输出: 2
示例 2:
输入: 8
输出: 2
说明: 8 的平方根是 2.82842...,
由于返回类型是整数,小数部分将被舍去。
解题思路:
这道题要求平方根,我们能想到的方法就是算一个候选值的平方,然后和x比较大小,为了缩短查找时间,我们采用二分搜索法来找平方根,这里属于之前总结的LeetCode Binary Search Summary 二分搜索法小结(http://www.cnblogs.com/grandyang/p/6854825.html)中的第三类的变形,找最后一个不小于目标值的数,代码如下:
C++解法一:
class Solution {
public:
int mySqrt(int x) {
if (x <= ) return x;
int left = , right = x;
while (left < right) {
int mid = left + (right - left) / ;
if (x / mid >= mid) left = mid + ;
else right = mid;
}
return right - ;
}
};
这道题还有另一种解法,是利用牛顿迭代法(https://zh.wikipedia.org/wiki/%E7%89%9B%E9%A1%BF%E6%B3%95),记得高数中好像讲到过这个方法,是用逼近法求方程根的神器,在这里也可以借用一下,可参见http://www.cnblogs.com/AnnieKim/archive/2013/04/18/3028607.html,因为要求x2 = n的解,令f(x)=x2-n,相当于求解f(x)=0的解,可以求出递推式如下:
xi+1=xi - (xi2 - n) / (2xi) = xi - xi / 2 + n / (2xi) = xi / 2 + n / 2xi = (xi + n/xi) / 2
C++解法二:
class Solution {
public:
int mySqrt(int x) {
if (x == ) return ;
double res = , pre = ;
while (abs(res - pre) > 1e-) {
pre = res;
res = (res + x / res) / ;
}
return int(res);
}
};
下面也是牛顿迭代法,写法更加简洁一些,注意为了防止越界,声明为长整型。
C++解法三:
class Solution {
public:
int mySqrt(int x) {
long res = x;
while (res * res > x) {
res = (res + x / res) / ;
}
return res;
}
};
LeetCode(69):x 的平方根的更多相关文章
- Java实现 LeetCode 69 x的平方根
69. x 的平方根 实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去. 示例 1: 输入: ...
- [leetcode] 69. x 的平方根(纯int溢出判断实现)
69. x 的平方根 非常简单的一个题,用二分法逼近求出ans即可,额外注意下溢出问题. 不过我要给自己增加难度,用long或者BigNum实现没意思,只能使用int类型 换句话当出现溢出时我们自己得 ...
- LeetCode 69 x 的平方根
链接:https://leetcode-cn.com/problems/sqrtx 实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数, ...
- [LeetCode]69. x 的平方根(数学,二分)
题目 https://leetcode-cn.com/problems/sqrtx 题解 方法一:牛顿迭代法 按点斜式求出直线方程(即过点Xn,f(Xn)),然后求出直线与x轴交点,即为Xn+1: 求 ...
- 字节笔试题 leetcode 69. x 的平方根
更多精彩文章请关注公众号:TanLiuYi00 题目 解题思路 题目要求非负整数 x 的平方根,相当于求函数 y = √x 中 y 的值. 函数 y = √x 图像如下: 从上图中,可以看出函数是单 ...
- Leetcode 69. Sqrt(x)及其扩展(有/无精度、二分法、牛顿法)详解
Leetcode 69. Sqrt(x) Easy https://leetcode.com/problems/sqrtx/ Implement int sqrt(int x). Compute an ...
- 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) 求平方根
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). Compute and return the square root of x. x is guaranteed to be a non-nega ...
- 力扣(LeetCode)69. x 的平方根
实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去. 示例 1: 输入: 4 输出: 2 示例 ...
随机推荐
- confluence 5.8.6升级到5.10.1
1.下载最新版 https://www.atlassian.com/software/confluence/download 2.上传至服务器 tar zxf atlassian-confluence ...
- app-web 开发 追溯debug
1.iphone5 运行vue项目时,方法格式:fun(){}这种格式容易不显示页面 2.vue未绑定上数据有可能是js文本过大,手机内存不足引起的 3.根据方法走向追溯debug 4.一定要用try ...
- 连接 sqlserver
提示错误:Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: 通过端口 1433 ...
- 20165237 2017-2018-2 《Java程序设计》第4周学习总结
20165237 2017-2018-2 <Java程序设计>第4周学习总结 教材学习内容总结 1.子类只能有一个父类,父类可以有多个子类. 2.子类继承父类的成员变量和方法. 3.开闭原 ...
- openstack Q版部署-----界面horizon安装(9)
一.界面的安装 控制节点安装软件包: yum install openstack-dashboard -y [root@linux-node1 ~]# vim /etc/openstack-dashb ...
- Struts2学习(二)
1.Struts2的Servlet的API的访问 1.1 完全解耦合的方式 ActionContext context = ActionContext.getContext( ); 通过conte ...
- P5238 整数校验器
题目地址:P5238 整数校验器 显然这道题不算难,细心即可AC 细节见代码中的注释 #include <bits/stdc++.h> #define ll long long using ...
- Linux就该这么学(1)-系统概述(学习笔记)
一.热门的Linux系统开源许可协议 GNU GPL(GNU General Public License,GNU 通用公共许可证) BSD(Berkeley Software Distributio ...
- 利用crash 分析软死锁问题【转】
转自:https://blog.csdn.net/divlee130/article/details/47806551 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog. ...
- Linux Makefile 生成 *.d 依赖文件及 gcc -M -MF -MP 等相关选项说明【转】
转自:https://blog.csdn.net/qq1452008/article/details/50855810 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog. ...