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. 百道CTF刷题记录(一)

    简介 最近在刷CTF题,主攻Web,兼职Misc Shiyanbar 0x01 简单的登陆题 简单概括: 考点: %00截断正则 CBC字节翻转攻击 难度: 难 WP:https://blog.csd ...

  2. 洛谷——P1231 教辅的组成

    P1231 教辅的组成 题目背景 滚粗了的HansBug在收拾旧语文书,然而他发现了什么奇妙的东西. 题目描述 蒟蒻HansBug在一本语文书里面发现了一本答案,然而他却明明记得这书应该还包含一份练习 ...

  3. 1005 Spell It Right (20)(20 point(s))

    problem Given a non-negative integer N, your task is to compute the sum of all the digits of N, and ...

  4. 利用python实现新浪微博爬虫

    第一个模块,模拟登陆sina微博,创建weiboLogin.py文件,输入以下代码: #! /usr/bin/env python # -*- coding: utf-8 -*- import sys ...

  5. DHTML参考手册

    中文DHTML参考手册 本dhtml教程由宏博内容管理系统为它的Smarty模板制作者收集的,目的是可以做出更加精美的模板.下面列出了由动态 HTML 定义的对象.DHTML中文参考手册,实用dhtm ...

  6. DataGridView、List<T>相关操作

    一.DataGridView数据转成DataTable 1.已绑定过数据源:DataTable dt = (dataGridView1.DataSource as DataTable) 2.未绑定过数 ...

  7. PHP大型电商网站秒杀思路

    秒杀/抢购 技术:高可用,高并发 市场:用户体验,曝光度,促销 秒杀放单独服务器,这样即使崩溃不影响网站其他功能. 高可用:双活. 高并发:负载均衡,安全过滤. 阿里云:云监控 分流,CDN加速 业务 ...

  8. 20162325 金立清 S2 W10 C19

    20162325 2017-2018-2 <程序设计与数据结构>第10周学习总结 认识 线性表和树两类数据结构,线性表中的元素是"一对一"的关系,树中的元素是" ...

  9. 细说React(一)

    React 是近期非常热门的一个前端开发框架. 这篇文章将介绍如何使用 React 来创建用户界面,希望能够起到抛砖引玉的效果. "React,  A JAVASCRIPT LIBRARY ...

  10. 解决 git push Failed to connect to 127.0.0.1 port 45463: 拒绝连接

    使用Github pull 代码突然报错: Failed to connect to 127.0.0.1 port 43421: Connection refused 使用 lsof 发现端口未被占用 ...