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下常用文件传输命令(转)
因为工作原因,需要经常在不同的服务器见进行文件传输,特别是大文件的传输,因此对linux下不同服务器间数据传输命令和工具进行了研究和总结.主要是rcp,scp,rsync,ftp,sftp,lftp, ...
- 使用java修改图片DPI
修改以后可以直接用PS打开看效果 全部使用rt下的类,无需下载其他jar包 import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.imag ...
- 【C#】C#创建Windows Service服务
目录结构: contents structure [+] 创建Windows服务 配置 安装Windows服务 在Visual Studio中调试 常见问题 最近写了一个TCP连接的程序,由于这种通信 ...
- Fastjson是一个Java语言编写的高性能功能完善的JSON库。
简介 Fastjson是一个Java语言编写的高性能功能完善的JSON库. 高性能 fastjson采用独创的算法,将parse的速度提升到极致,超过所有json库,包括曾经号称最快的jackson. ...
- Jquery 选择器 详解 js 判断字符串是否包含另外一个字符串
Jquery 选择器 详解 在线文档地址:http://tool.oschina.net/apidocs/apidoc?api=jquery 各种在线工具地址:http://www.ostools ...
- 《JAVA与模式》之模板模式(转载)
模板方法在servlet中的应用:http://www.cnblogs.com/java-my-life/archive/2012/05/14/2495235.html 原文出处:http://blo ...
- please verify the preference field with the prompt:Tomcat JDK name
使用MyEclipse的Tomcat的时候出现下面的问题: a configuration error occurred during startup. please ve ...
- no OPENSSL_Applink错误的解决方法
原文链接: http://www.cnblogs.com/sdnyzhl/archive/2012/12/11/2813210.html 自己按照openssl中介绍的编译,安装openssl,其间编 ...
- Java使用reids,以及redis与shiro集成
什么是redis:redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(so ...
- 第2章 Python基础-字符编码&数据类型 字典 练习题
1.写代码,有如下字典,按照要求实现每一个功能,dic = {'k1':'v1','k2':'v2','k3':[11,22,33]} 请循环输出所有的 key dic = {'k1':'v1','k ...