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. 请爱护你的JTAG烧录口---记录

        排除了下载线的问题后,还是不能访问FPGA的JTAG口,那么很有可能你的FPGA芯片的JTAG口已经损坏.此时请用万用表检查TCK,TMS,TDO和Tdi是否和GND短路,如果任何一个信号对地 ...

  2. 1021 Deepest Root (25)(25 point(s))

    problem A graph which is connected and acyclic can be considered a tree. The height of the tree depe ...

  3. DMA

    DMA:如果将一串字符串通过串口传送到外设中去,用传统的方法,则CPU将不断的去扫描UTSTAT这个寄存器,在字符发送期间,CPU将不能做任何其他事情.为了解决这个问题,则在诞生了DMA CPU只需要 ...

  4. Java设计模式GOF之6大设计原则

    Java设计模式GOF之6大设计原则原则 1.开闭原则(Open Close Principle) 一个软件实体如类.模块和函数应该对扩展开放,对修改关闭. 开闭原则是面向对象的可复用设计的第一块基石 ...

  5. [BZOJ5338][TJOI2018]xor(可持久化Trie)

    可持久化Trie模板题. 建两种可持久化Trie,每个点两棵,一棵对DFS求前缀和,一棵对祖先求前缀和. 或者树剖,不好写多少还多个log. #include<cstdio> #inclu ...

  6. 奇妙的音乐-----WriteUp

    据说flag就藏在这段音乐中,请仔细听. 格式:CTF{} 解题链接:http://ctf5.shiyanbar.com/crypto/123.zip 下载压缩包后里面有个音乐的zip文件  但是加密 ...

  7. Hihocoder #1081 最短路径一 dijkstra

    #1081 : 最短路径·一 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 万圣节的早上,小Hi和小Ho在经历了一个小时的争论后,终于决定了如何度过这样有意义的一天—— ...

  8. Redis系列之(一):10分钟玩转Redis

    1. Redis介绍 Redis是一个开源的使用ANSI C语言编写.基于内存的Key-Value数据库. 它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集 ...

  9. 华为S5300系列交换机V100R006SPH019升级补丁

    S5300_V100R006SPH019.pat 附件: 链接:https://pan.baidu.com/s/1M1S5amGGViUieSp8lJ9psw  密码:sexx

  10. POJ 3580 SuperMemo (splay tree)

    SuperMemo Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 6841   Accepted: 2268 Case Ti ...