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. Json格式String类型字符串转为Map工具类

    package agriculture_implement.util; import com.google.gson.Gson; import com.google.gson.JsonSyntaxEx ...

  2. Xamarin 2017.10.9更新

     Xamarin 2017.10.9更新 本次更新主要解决了一些bug.Visual Studio 2017升级到15.4获得新功能.Visual Studio 2015需要工具-选项-Xamarin ...

  3. Swift2.0语言教程之函数的返回值与函数类型

    Swift2.0语言教程之函数的返回值与函数类型 Swift2.0中函数的返回值 根据是否具有返回值,函数可以分为无返回值函数和有返回值函数.以下将会对这两种函数类型进行讲解. Swift2.0中具有 ...

  4. [HDU6198]number number number

    题目大意: 给定一个数k表示你可以从包括0的斐波那契数列中任取k个数,得到它们的和.求最小的不能得到的自然数. 思路: 打表找规律,可以发现答案为f(2k+3)-1,然后用公式f(i)=f(i/2)* ...

  5. 协议栈中使用crc校验函数

    CRC校验介绍:循环冗余校验码,原理是多项式除法 ZigBee协议栈:能够使zigbee节点相互之间组网,数据传输,数据获取,数据显示 思路以及步骤: 1.因为IAR的程序是用c写的,所以上网查找如何 ...

  6. Codeforces Round #353 (Div. 2) E. Trains and Statistic dp 贪心

    E. Trains and Statistic 题目连接: http://www.codeforces.com/contest/675/problem/E Description Vasya comm ...

  7. Codeforces Beta Round #97 (Div. 1) A. Replacement 水题

    A. Replacement 题目连接: http://codeforces.com/contest/135/problem/A Description Little Petya very much ...

  8. Codeforces Round #298 (Div. 2) E. Berland Local Positioning System 构造

    E. Berland Local Positioning System Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.c ...

  9. HP Microserver Gen8 Processor FAQ

    http://homeservershow.com/forums/index.php?/topic/6596-hp-microserver-gen8-processor-faq/ This guide ...

  10. Android设备运用Clockworkmod Recovery恢复模式安装定制的Rom

    Clockworkmod Recovery是一个由Cyanogen团队开发的用于Android设备的第三方定制Recovery恢复模式,也称为CWM Recovery,具体它有什么用处呢?请看关于Go ...