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) 解题报告的更多相关文章

  1. LeetCode: Combination Sum 解题报告

    Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...

  2. 【LeetCode】Permutations 解题报告

    全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...

  3. LeetCode - Course Schedule 解题报告

    以前从来没有写过解题报告,只是看到大肥羊河delta写过不少.最近想把写博客的节奏给带起来,所以就挑一个比较容易的题目练练手. 原题链接 https://leetcode.com/problems/c ...

  4. LeetCode: Sort Colors 解题报告

    Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...

  5. 【LeetCode】69. Sqrt(x) 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:库函数 方法二:牛顿法 方法三:二分查找 日 ...

  6. C++版 - Leetcode 69. Sqrt(x) 解题报告【C库函数sqrt(x)模拟-求平方根】

    69. Sqrt(x) Total Accepted: 93296 Total Submissions: 368340 Difficulty: Medium 提交网址: https://leetcod ...

  7. LeetCode: Permutation Sequence 解题报告

    Permutation Sequence https://oj.leetcode.com/problems/permutation-sequence/ The set [1,2,3,…,n] cont ...

  8. Leetcode:Interleaving String 解题报告

    Interleaving StringGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For ...

  9. Leetcode:Scramble String 解题报告

    Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...

随机推荐

  1. 给我一对公钥和私钥,我就能破解此RSA

    RSA密码系统如果暴露了一套公钥和私钥,那么这套密码系统就全部失效了.因为根据公钥和私钥可以完成大整数的分解.暴露了两个质数. 记公钥为e,私钥为d,因为ed%phi=1,所以就得到了一个k=ed-1 ...

  2. Xcode8 带来的新特性和坑

    Xcode8新特性 Interface Builder 随着14年的iPhone6和6P出来之后,iPhone的屏幕尺寸也越来越多,屏幕适配是一个需要解决的问题,以后不一定苹果又出什么尺寸的iPhon ...

  3. php如何在某个时间上加一天?一小时? 时间加减(转)

    <?php date_default_timezone_set('PRC'); //默认时区 echo "今天:",date("Y-m-d",time() ...

  4. CentOS 7 systemd的坑

    一.概述 在从 CentOS 6 迁移到 CentOS 7 的过程中,可能有一些地方需要调整,最显著的地方莫过于 systemd 带来的改变,不同的管理服务的方式,不同的日志方式,设置时区,时间等等. ...

  5. sql server 2008评估期已到的解决办法

    点击开始-所有程序-Microsoft SQL Server 2008-配置工具-SQL Server 安装中心然后点击左侧的维护,在点击右侧的版本升级,接着按照提示一直点下一步,到产品密钥的时候输入 ...

  6. Html中 <tr> 标签的隐藏与显示

    TR标签的隐藏与显示:block(显示)和none(隐藏) <tr  style="display:none"> <tr id="sample" ...

  7. php截取字符去掉最后一个字符

    $str="中国.美国.俄罗斯.德国."$str=substr($str,0,-1); 输出结果为:中国.美国.俄罗斯.德国

  8. Android视频播放和横竖屏切换

    最近做了一个项目,里面用到了视频播放这一块,当时想考虑Vitamio,demo也做了出来,但是后来发现它是商业收费的,并且收费相当可观,所以只能放弃了.然后找到了ijkPlayer,功能也很强大,最终 ...

  9. 跟我学SharePoint 2013视频培训课程——探索默认的列表和库(6)

    课程简介 第6天,探索默认的列表和库. 视频 SharePoint 2013 交流群 41032413

  10. PHP与MYSQL中UTF8 中文排序例子

    1. 需要在php数组中用中文排序,但是一般使用utf8格式的文件,直接用asort排序不行.用gbk和gb2312可以.这跟几种格式的编码有关系.gbk和gb2312本身的编码就是用拼音排序的. 代 ...