LeetCode:Sqrt(x) 解题报告
Sqrt(x)
Implement int sqrt(int x)
.
Compute and return the square root of x.
SOLUTION 1:
参见:二分法总结,以及模板:http://t.cn/RZGkPQc
public class Solution {
public int sqrt(int x) {
if (x == 1 || x == 0) {
return x;
} int left = 1;
int right = x; while (left < right - 1) {
int mid = left + (right - left) / 2;
int quo = x / mid; if (quo == mid) {
return quo;
// mid is too big
} else if (quo < mid) {
right = mid;
} else {
left = mid;
}
} return left;
}
}
其实这里有一个非常trick地地方:
就是当循环终止的时候,l一定是偏小,r一定是偏大(实际的值是介于l和r之间的):
比如以下的例子,90开根号是9.48 按照开方向下取整的原则, 我们应该返回L.
以下展示了在循环过程中,L,R两个变量的变化过程
1. System.out.println(sqrt(90));
L R
1 45
1 23
1 12
6 12
9 12
9 10
9
2. System.out.println(sqrt(20));
1 10
1 5
3 5
4 5
4
3. System.out.println(sqrt(3));
1 2
GITHUB:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/divide2/Sqrt.java
LeetCode:Sqrt(x) 解题报告的更多相关文章
- LeetCode: Combination Sum 解题报告
Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...
- 【LeetCode】Permutations 解题报告
全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...
- LeetCode - Course Schedule 解题报告
以前从来没有写过解题报告,只是看到大肥羊河delta写过不少.最近想把写博客的节奏给带起来,所以就挑一个比较容易的题目练练手. 原题链接 https://leetcode.com/problems/c ...
- LeetCode: Sort Colors 解题报告
Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...
- 【LeetCode】69. Sqrt(x) 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:库函数 方法二:牛顿法 方法三:二分查找 日 ...
- C++版 - Leetcode 69. Sqrt(x) 解题报告【C库函数sqrt(x)模拟-求平方根】
69. Sqrt(x) Total Accepted: 93296 Total Submissions: 368340 Difficulty: Medium 提交网址: https://leetcod ...
- LeetCode: Permutation Sequence 解题报告
Permutation Sequence https://oj.leetcode.com/problems/permutation-sequence/ The set [1,2,3,…,n] cont ...
- Leetcode:Interleaving String 解题报告
Interleaving StringGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For ...
- Leetcode:Scramble String 解题报告
Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...
随机推荐
- linux shell 脚本攻略学习16--wc命令详解,tree命令详解
在文本处理的工作中,统计文件的行数,单词数和字符数非常有用.而对于开发人员本身来说,统计LOC(line of code ,代码行数)是一件重要的工作.linux中有什么命令可以帮助我们做统计呢?没错 ...
- 【总结】java regex 正则表达式 提取数字和去除数字,过滤数字,提取价格
@Test public void test33() { String phoneString = "哈哈,13888889999"; // 提取数字 Pattern patter ...
- 【LeetCode】174. Dungeon Game
Dungeon Game The demons had captured the princess (P) and imprisoned her in the bottom-right corner ...
- 【SqlServer】Sql Server 支持的数据类型
在计算机中数据有两种特征:类型和长度.所谓数据类型就是以数据的表现方式和存储方式来划分的数据的种类. 在SQL Server 中每个变量.参数.表达式等都有数据类型.系统提供的数据类型分为几大类 ...
- sqlite 常用命令
1)创建数据库文件: >sqlite3 /home/test.db 回车 就生成了一个test.db在/home路径下. 这样同时也SQLite3加载了这个test.db 2)用.help可以看 ...
- 怎样为你的CSDN博客增加百度统计
曾经CSDN使用的 量子统计 能够非常好的统计我们的博客的訪问数量.地域等等信息,可是不知道后来为什么不在使用了.那么怎样找到 一种替换的方式那? 下边,就给大家介绍一下怎样使用百度统计. 百度统计账 ...
- 温故而知新 原来 cheerio 还可以操作XML
- JQ:命令行 json 解析神器 —— 命令行的Jsonview
- 转:ECharts图表组件之简单关系图:如何轻松实现另类站点地图且扩展节点属性实现点击节点页面跳转
站点地图不外乎就是罗列一个网站的层次结构,提炼地讲就是一个关系结构图.那么我们如何巧用ECharts图表组件内的简单关系结构图来实现一个站点的地图结构呢?另外如何点击某个节点的时候实现页面跳转呢? 针 ...
- unity, 2d rope
https://www.youtube.com/watch?v=l6awvCT29yU