Reference: http://blog.csdn.net/lbyxiafei/article/details/9375735

 题目

Implement int sqrt(int x).

Compute and return the square root of x.

题解

这道题很巧妙的运用了二分查找法的特性,有序,查找pos(在这道题中pos=value),找到返回pos,找不到返回邻近值。

因为是求数x(x>=0) 的平方根, 因此,结果一定是小于等于x且大于等于0,所以用二分查找法肯定能搜到结果。

以每一次的mid的平方来与target既数x相比:

如果mid*mid == x,返回mid;

如果mid*mid < x,那么说明mid过小,应让low = mid+1,在右边继续查找

如果mid*mid > x,那么说明mid过大,应让high = mid-1,在左边继续查找

若x无法开整数根号(在上述查找中没有找到),那么我们仍然可以利用之前对二分查找法总结的技巧,当target值不在数组中,low指向大于target的那个值,high指向小于target的那个值,由于我们需要向下取整的结果,所以我们返回high指向的值(这里high指向的值和high的值是同一个值),这个值就是所求得最接近起开根号结果的整数值。

因为leetcode的test case x=2147395599,在算mid*mid的时候造成溢出,所以mid不能使用int型来接,要使用long型防止溢出(Java中Integer型的范围:-2147483648 到2147483648)

代码为:

 1 public int sqrt(int x) {
 2         int low = 0;
 3         int high = x;
 4         while(low<=high){
 5             long mid = (long)(low + high)/2;
 6             if(mid*mid < x)
 7                 low = (int)mid + 1;
 8             else if(mid*mid > x)
 9                 high = (int)mid - 1;
             else
                 return (int)mid;
         }
         return high;
     }

Sqrt(int x) leetcode java的更多相关文章

  1. N-Queens II leetcode java

    题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...

  2. ZigZag Conversion leetcode java

    题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...

  3. [LeetCode][Java]Candy@LeetCode

    Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  4. [Leetcode][JAVA] Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  5. Bulb Switcher (leetcode java)

    问题描述: There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off ...

  6. Single Number II leetcode java

    问题描述: Given an array of integers, every element appears three times except for one. Find that single ...

  7. Scramble String leetcode java

    题目: Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty subs ...

  8. Regular Expression Matching leetcode java

    题目: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...

  9. Reverse Words in a String leetcode java

    题目: Given an input string, reverse the string word by word. For example, Given s = "the sky is ...

随机推荐

  1. react篇章-React Props-Props 验证

    React.PropTypes 在 React v15.5 版本后已经移到了 prop-types 库. <script src="https://cdn.bootcss.com/pr ...

  2. Django学习笔记--通用列表和详细信息视图

    根据教程写完代码后,点击All books也一直跳转到index的页面 我打开了F12调试,看到点击没有出现book_list的代码,觉得应该是url的路径写得不对,但是跟教程代码对比了下,并没有发现 ...

  3. hdu-5023线段树刷题

    title: hdu-5023线段树刷题 date: 2018-10-18 13:32:13 tags: acm 刷题 categories: ACM-线段树 概述 这道题和上次做的那道染色问题一样, ...

  4. Linux-CentOs7-svn安装及钩子配置

    做个svn的教程 首先进入test目录下,新建一个svn目录,准备做svn测试cd /testmkdir svncd svn然后使用yum安装svn,这里就不使用编译安装了,这玩意只要能用就行,版本无 ...

  5. 减少TIME_WAIT连接状态

    减少TIME_WAIT连接状态.网络上已经有不少相关的介绍,大多是建议: shell> sysctl net.ipv4.tcp_tw_reuse=1 shell> sysctl net.i ...

  6. python opencv3 cornerHarris 角点检测

    git:https://github.com/linyi0604/Computer-Vision 角点也是处在一个无论框框往哪边移动 框框内像素值都会变化很大的情况而定下来的点 如果框框水平方向上移动 ...

  7. Ubuntu安装redis和redis-php扩展

    通过apt-get安装的redis使用方法 sudo apt-get install redis-server sudo apt-get install php-redis vim /etc/redi ...

  8. java 反编译 android 反编译

    1. jad http://varaneckas.com/jad/jad158e.linux.intel.zip  下载jad, 给jad运行权限 ,运行 chmod a+x ./jad ./jad ...

  9. ActiveMQ Cluster (ActiveMQ 集群) 配置

    构建高可用的ActiveMQ系统在生产环境中是非常重要的,对于这个apache的消息中间件实现高可用非常简单,只要在Apache ActiveMQ单点基本配置基础上做一次配置变更(如果在一台设备上部署 ...

  10. Mina 断线重连

    Mina 断线重连 定义:这里讨论的Mina 断线重连是指使用mina作为客户端软件,连接其他提供Socket通讯服务的服务器端.Socket服务器可以是Mina提供的服务器,也可以是C++提供的服务 ...