Sqrt(x) 解答
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) 解答的更多相关文章
- 海边直播目标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( ...
- [问题2014A09] 解答
[问题2014A09] 解答 通过简单的计算可得 \[(AB)^2=9AB,\cdots\cdots(1)\] 将 (1) 式的右边移到左边, 并将 \(A,B\) 分别提出可得 \[A(BA-9I ...
- [问题2014S13] 解答
[问题2014S13] 解答 (1) 先证必要性:若 \(A=LU\) 是 非异阵 \(A\) 的 \(LU\) 分解,则 \(L\) 是主对角元全部等于 1 的下三角阵,\(U\) 是主对角元全部 ...
- 《数据结构与算法分析:C语言描述_原书第二版》CH2算法分析_课后习题_部分解答
对于一个初学者来说,作者的Solutions Manual把太多的细节留给了读者,这里尽自己的努力给出部分习题的详解: 不当之处,欢迎指正. 1. 按增长率排列下列函数:N,√2,N1.5,N2,N ...
- 快学Scala习题解答—第一章 基础
1 简介 近期对Scala比较感兴趣,买了本<快学Scala>,感觉不错.比<Programming Scala:Tackle Multi-Core Complexity on th ...
- 应用留数定理计算实积分 $\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 ...
- MT【256】2016四川高考解答压轴题
(2016四川高考数学解答压轴题)设函数$f(x)=ax^2-a-\ln x,a\in R$. 1)讨论$f(x)$的单调性;2)确定$a$的所有可能值,使得$f(x)>\dfrac{1}{x} ...
- LeetCode题目解答
LeetCode题目解答——Easy部分 Posted on 2014 年 11 月 3 日 by 四火 [Updated on 9/22/2017] 如今回头看来,里面很多做法都不是最佳的,有的从复 ...
- LeetCode算法题目解答汇总(转自四火的唠叨)
LeetCode算法题目解答汇总 本文转自<四火的唠叨> 只要不是特别忙或者特别不方便,最近一直保持着每天做几道算法题的规律,到后来随着难度的增加,每天做的题目越来越少.我的初衷就是练习, ...
随机推荐
- [置顶] 【cocos2d-x入门实战】微信飞机大战之十三:游戏场景过渡
原创作品,转载请标明:http://blog.csdn.net/jackystudio/article/details/12082043 游戏是实现了,但是如果有个欢迎界面和一个结束界面就更好了. 欢 ...
- js为鼠标添加右击事件
<script language="javascript"> /*document.oncontextmenu=Youji;*/ //为当前文档添加鼠标右击事件,防 ...
- Redis安全
安全 执行在可信环境 Redis的安全设计是在"Redis执行在可信环境"这个前提下做出的.在生产环境执行时不能同意外界直接连接到Redisserver上.而应该通过应用程序进行中 ...
- Activity中的startActivityResult,setResult,finish,onActivityResult的关系
一:首先图示: 二:代码: 1:方法selectName public void selectName(View view){ Intent intent = new Intent(this,Name ...
- ibatis之##与$$的 使用
/** 主要讲一下ibatis中$$的使用: 是为了传递参数; 参数一定在Action层用''包裹起来: */ List <SysRole> userList= systemService ...
- extern C的作用详解
extern "C"的主要作用就是为了能够正确实现C++代码调用其他C语言代码.加上extern "C"后,会指示编译器这部分代码按C语言的进行编译,而不是C+ ...
- using 关键字给类和名称空间指定别名
1. using System; using System.Text; namespace DLL { public class Class1 { public void showStr() { Co ...
- VMware 虚拟机使用RedHat,出现 connect: Network is unreachable解決方法
http://www.linuxidc.com/Linux/2015-02/113119.htm http://www.osyunwei.com/archives/7829.html
- 通过jqueryui实现邮件提示
//js代码$(function () { var availableTags = ["@qq.com", "@gmail.com", "@126.c ...
- Django数据迁移
http://www.ziqiangxuetang.com/django/django-data-migration.html