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. 【转】IOCP创建

    转自:http://www.cmnsoft.com/wordpress/?p=248 感谢原作者.我在此整理一下: 完成端口(IOCP)是WINDOWS平台上特有的一种技术.要使用IOCP技术,就要用 ...

  2. 【LeetCode】208. Implement Trie (Prefix Tree)

    Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Note:You ...

  3. webpack window下配置的hello world

    峰回路转 一区九折 先看效果:(此效果是webpack执行完之后会生成build文件夹已经它下面的index.html,点击index.html就是下图的效果)

  4. 【Hibernate】解析hibernate中的缓存

    Hibernate中的缓存一共有三种,一级缓存.二级缓存.查询缓存.缓存除了使用Hibernate自带的缓存,还可以使用redis进行缓存,或是MongoDB进行缓存. 所使用的Demo: User. ...

  5. python 生成图表

    python写入excel(xlswriter)--生成图表 折线图 # -*- coding:utf-8 -*- import xlsxwriter # 创建一个excel workbook = x ...

  6. Java NIO.2 —— 文件或目录删除操作

    文件删除 删除单个文件的操作很简单,如果要删除一个目录树的话,需要实现FileVisitor 接口然后递归地调用delete() 或deleteIfExists()方法.在看代码之前,需要注意一下问题 ...

  7. Linux下查找指定日期的文件

    一.背景 Linux服务器的一个目录里,每天产生海量的文件.为了防止磁盘被写满. 决定每天删除部分老文件.OK,问题来了,如何过滤出指定日期的文件? 二.强大的Linux 一行代码搞定: ls --f ...

  8. 使用 MD5 加密 去重对插入的影响

    现在有3000条数据,需要插入到数据库中去,使用的是对链接进行MD5加密, hashcode = md5(str(item_url))然后在数据库中设置 hashcode 为UNIQUE索引 3000 ...

  9. Android Activity全面解析

    Android Activity全面解析 首先,就从Android四大组件Activity开始. 1.Activity生命周期方法完全解析   activity_lifecycle.png 1).on ...

  10. MySQL 示例数据库 employees 详解

    [引子] IT这一行在我看来是比较要求动手能力的,但是人非生而知之:人们身上的技能除了一些本能之外,大多都是通过学习而得到的. 前一段时间一直在整理素材,写一个关于explain 的系列文章:在一开始 ...