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 ...
随机推荐
- Language and Compiler Features Since Delphi 7
from: http://edn.embarcadero.com/cn/article/34324 Language and Compiler Features Since Delphi 7 In ...
- 【RS】Improving Implicit Recommender Systems with View Data - 使用浏览数据提升隐式推荐系统
[论文标题]Improving Implicit Recommender Systems with View Data(IJCAI 18) [论文作者]Jingtao Ding , Guanghui ...
- java+win7+eclipse+Maven+sikuli 配置总结---图形脚本语言
简介:Sikuli 是一种新颖的图形脚本语言,或者说是一种另类的自动化测试技术.它与我们常用的自动化测试技术(工具)有很大的区别. 关于配置,一直是一个问题,下面做个总体介绍,用sikuli也有几个月 ...
- 运维人员20道必会iptables面试题
1.详述iptales工作流程以及规则过滤顺序? iptables过滤的规则顺序是由上至下,若出现相同的匹配规则则遵循由上至下的顺序 2.iptables有几个表以及每个表有几个链? Iptables ...
- 阿里面试的一点感受 阿里ali片式经历和面试题
阿里面试的一点感受 <!-- [废话开始] 百度实习三个月,明天就要离职了,感觉还挺开心的,同事们都很照顾我,Boss也比较欣赏我,我很满足了.掐指一算,这大四其实也没几个月了,同事们都在感叹大 ...
- jQuery UI API - 可拖拽小部件(Draggable Widget)(转)
所属类别 交互(Interactions) 用法 描述:允许使用鼠标移动元素. 版本新增:1.0 依赖: UI 核心(UI Core) 部件库(Widget Factory) 鼠标交互(Mouse I ...
- android 当ListView滚动时自动调用 onCheckedChanged 导致CheckBox 状态不停变化 的解决办法
今天在做一个含有CheckBox 的ListView时,发现当初始化CheckBox的状态后, 滚动ListView,其中CheckBox 的选中状态不停的发生变化.最后发现原因是 ListView滚 ...
- ios面试题来一波
一.如果让你实现属性的weak,如何实现的? PS: @property 等同于在.h文件中声明实例变量的get/set方法, 而其中property有一些关键字,其中就包括weak,atomic的. ...
- Postman 网络调试工具
1.Postman 简介 Postman 是一款功能强大的网页调试与发送网页 HTTP 请求的工具.我们可以用来很方便的模拟 get 或者 post 或者其他方式的请求来调试接口. 官网下载地址 Po ...
- [转]python数据持久存储:pickle模块的基本使用
python的pickle模块实现了基本的数据序列和反序列化.通过pickle模块的序列化操作我们能够将程序中运行的对象信息保存到文件中去,永久存储:通过pickle模块的反序列化操作,我们能够从文件 ...