LeetCode 69. Sqrt(x) (平方根)
Implement int sqrt(int x)
.
Compute and return the square root of x.
x is guaranteed to be a non-negative integer.
Example 1:
Input: 4
Output: 2
Example 2:
Input: 8
Output: 2
Explanation: The square root of 8 is 2.82842..., and since we want to return an integer, the decimal part will be truncated.
题目标签:Math
题目给了我们一个x, 让我们找到平方根。
可以利用binary search 来做,首先找到一个范围,不需要是从0 到 x。因为 sqrt(x) 一定是小于等于 x / 2 + 1的。
所以起始范围为 0 到 x / 2 + 1;
还要注意 sq = mid * mid。 这里要检查 overflow。
Java Solution:
Runtime beats 53.10%
完成日期:06/12/2017
关键词:Binary Search
关键点:检查overflow
class Solution
{
public int mySqrt(int x)
{
int left = 0;
int right = x / 2 + 1; while(left <= right)
{
int mid = left + (right - left)/2;
int sq = mid * mid; if(mid != 0 && sq / mid != mid) // check overflow
{
right = mid - 1; // meaning mid is too big, go to left part
continue;
} if(sq == x)
return mid;
else if(sq < x)
left = mid + 1;
else
right = mid - 1; } return right;
}
}
参考资料:N/A
LeetCode 题目列表 - LeetCode Questions List
题目来源:https://leetcode.com/
LeetCode 69. Sqrt(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 ...
- 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. 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 x 的平方根
链接:https://leetcode-cn.com/problems/sqrtx 实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数, ...
- Leetcode 69. Sqrt(x)
Implement int sqrt(int x). 思路: Binary Search class Solution(object): def mySqrt(self, x): "&quo ...
- (二分查找 拓展) 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 ...
随机推荐
- 关于Python多线程condition变量的应用
''' 所谓条件变量,即这种机制是在满足了特定的条件后,线程才可以访问相关的数据. 它使用Condition类来完成,由于它也可以像锁机制那样用,所以它也有acquire方法和release方法,而且 ...
- JPQL 模糊查询,查询条件拼接(like使用)
@Transactional public List<ViewCorplist2> findAllCorpsLikeK(String kw) { System.out.println(kw ...
- DB2 系统命令与配置参数大全
主要包括4个部分,分别为: DB2 系统命令 DB2 数据库管理器配置参数 DB2 数据库系统配置参数 DB2 管理服务器(DAS)配置参数DB2 系统命令 dasauto - 自动启动 DB2 管理 ...
- vue组件---自定义事件
首先简单回顾下组件事件及组件的复用 demo1:按钮事件 <div class="button_area"> <button-area></butto ...
- 调用微信扫一扫功能,踩坑'invalid signature'
在vue项目中,调用微信扫一扫功能,在安卓系统下完全正常,ios系统下却报错'invalid signature'的错误,这可能令许多小伙伴困惑,经过查询大量博客相关资料,才找到了解决的方法. 原因: ...
- Python之IO编程
前言:由于程序和运行数据是在内存中驻留的,由CPU这个超快的计算核心来执行.当涉及到数据交换的地方,通常是磁盘.网络等,就需要IO接口.由于CPU和内存的速度远远高于外设的速度,那么在IO编程中就存在 ...
- 队列的头函数使用C++
queue queue模板类的定义在<queue>头文件中. 与stack模板类很相似,queue模板类也需要两个模板参数,一个是元素类型,一个容器类型,元素类型是必要的,容器类型是可选的 ...
- 创建和获取cookie
创建和获取cookie 制作人:全心全意 cookie:在互联网中,cookie是小段的文本信息,在网络服务器上生成,并发送给浏览器.通过使用cookie可以标识用户身份,记录用户名和密码,跟踪重复用 ...
- LINUX-系统信息
系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统部件 - (SMBIOS ...
- js 防止重复提交表单
var addFlag = true; function addQuestion(){ if(!addFlag){ return; } addFlag = false; //执行更新操作 jQuery ...