Question

Implement int sqrt(int x).

Compute and return the square root of x.

Solution 1 -- O(log n)

常规做法是用的binary search。注意这里mid一定要是long类型,否则mid * mid会溢出。

 public class Solution {
public int mySqrt(int x) {
// Use long instead of int
long start = 1, end = x, mid;
while (start + 1 < end) {
mid = (end - start) / 2 + start;
long tmp = mid * mid;
if (tmp == x) {
return (int) mid;
} else if (tmp < x) {
start = mid;
} else {
end = mid;
}
}
if (end * end <= x)
return (int) end;
return (int) start;
}
}

Solution 2 -- Constant Time

我们可以通过以下数学公式缩小问题规模:

因此,问题转换成如何求 log2N

注意到对于任何数,它的binary representation可以表示为Σ2i

所以 log2N 的取值范围为[i, i + 1] i 为最高位的位数。

因此我们就缩小了二分查找的范围。

 public class Solution {
public int mySqrt(int x) {
int num = x, digit = 0;
while (num > 0) {
num = num >> 1;
digit++;
}
int candidate1 = (int) Math.pow(2, 0.5 * digit);
int candidate2 = (int) Math.pow(2, 0.5 * (digit - 1));
long start = candidate2, end = candidate1, mid;
while (start + 1 < end) {
mid = (end - start) / 2 + start;
long tmp = mid * mid;
if (tmp == x)
return (int) mid;
if (tmp < x)
start = mid;
else
end = mid;
}
if (end * end <= x)
return (int) end;
return (int) start;
}
}

Sqrt(x) 解答的更多相关文章

  1. 海边直播目标2017全国初中数学竞赛班课堂测试题解答-The Final

    1. 设函数 $f(x) = 2^x(ax^2 + bx + c)$ 满足等式 $f(x+1) - f(x) = 2^x\cdot x^2$, 求 $f(1)$. 解答: 由 $f(x) = 2^x( ...

  2. [问题2014A09] 解答

    [问题2014A09]  解答 通过简单的计算可得 \[(AB)^2=9AB,\cdots\cdots(1)\] 将 (1) 式的右边移到左边, 并将 \(A,B\) 分别提出可得 \[A(BA-9I ...

  3. [问题2014S13] 解答

    [问题2014S13]  解答 (1) 先证必要性:若 \(A=LU\) 是 非异阵 \(A\) 的 \(LU\) 分解,则 \(L\) 是主对角元全部等于 1 的下三角阵,\(U\) 是主对角元全部 ...

  4. 《数据结构与算法分析:C语言描述_原书第二版》CH2算法分析_课后习题_部分解答

    对于一个初学者来说,作者的Solutions Manual把太多的细节留给了读者,这里尽自己的努力给出部分习题的详解: 不当之处,欢迎指正. 1.  按增长率排列下列函数:N,√2,N1.5,N2,N ...

  5. 快学Scala习题解答—第一章 基础

    1 简介 近期对Scala比较感兴趣,买了本<快学Scala>,感觉不错.比<Programming Scala:Tackle Multi-Core Complexity on th ...

  6. 应用留数定理计算实积分 $\dps{I(x)=\int_{-1}^1\frac{\rd t}{\sqrt{1-t^2}(t-x)}\ (|x|>1,x\in\bbR)}$ [华中师范大学2010年复变函数复试试题]

    应用留数定理计算实积分 $\dps{I(x)=\int_{-1}^1\frac{\rd t}{\sqrt{1-t^2}(t-x)}\ (|x|>1,x\in\bbR)}$ [华中师范大学2010 ...

  7. MT【256】2016四川高考解答压轴题

    (2016四川高考数学解答压轴题)设函数$f(x)=ax^2-a-\ln x,a\in R$. 1)讨论$f(x)$的单调性;2)确定$a$的所有可能值,使得$f(x)>\dfrac{1}{x} ...

  8. LeetCode题目解答

    LeetCode题目解答——Easy部分 Posted on 2014 年 11 月 3 日 by 四火 [Updated on 9/22/2017] 如今回头看来,里面很多做法都不是最佳的,有的从复 ...

  9. LeetCode算法题目解答汇总(转自四火的唠叨)

    LeetCode算法题目解答汇总 本文转自<四火的唠叨> 只要不是特别忙或者特别不方便,最近一直保持着每天做几道算法题的规律,到后来随着难度的增加,每天做的题目越来越少.我的初衷就是练习, ...

随机推荐

  1. Bootstrap--本地安装使用

    1.到官网下载:http://v2.bootcss.com 2.下载后是一个压缩文件,把它放在相应的工作目录下,然后解压. 3.新建一个测试文件,然后导入两个文件.

  2. cobol语言基础培训教程

    COBOL 是Common Business Oriented Language 的缩写.它不仅是商业数据处理的理想语言,而且广泛用于数据管理领域,因此COBOL 语言也被称为”用于管理的语言”. 一 ...

  3. float position的測试案例

    依据<a target=_blank href="http://blog.csdn.net/goodshot/article/details/44348525">htt ...

  4. 如何单独编译Android源代码中的模块

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6566662 第一次下载好Android源代码工 ...

  5. 支持多QQ登录的软件

    支持多QQ登录,批量加好友,批量回复QQ消息,当然也能接收 下载链接:多QQ登录软件

  6. asp.net服务器向客户端弹出对话框,但不使页面边白板

    1: using System; 2: using System.Collections.Generic; 3: using System.Linq; 4: using System.Web; 5: ...

  7. .NET程序猿 - 提升幸福感的组件一览

    1.Newtonsoft.Json.net 操作JSON最简便的方式.  .Net 3.5开始,Framework集成Json序列化器:JavaScriptSerializer,然而Json.net给 ...

  8. Android与JS混编(js调用android相机扫描二维码)

    参考demo http://www.cnblogs.com/mythou/p/3280023.html        项目源码: https://github.com/weifengzz/Androi ...

  9. Objective-C 字符串

    #import <UIKit/UIKit.h> #import "AppDelegate.h" int main(int argc, char * argv[]) { ...

  10. apache 反向代理配置(ubuntu)

    1.配置apache2的站点文件 cd /etc/apache2/site-avaliable sudo vim edy.conf 具体配置如下: # 反向代理配置 # 监听所有80端口的访问 < ...