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算法题目解答汇总 本文转自<四火的唠叨> 只要不是特别忙或者特别不方便,最近一直保持着每天做几道算法题的规律,到后来随着难度的增加,每天做的题目越来越少.我的初衷就是练习, ...
随机推荐
- Maven .m2 setting.xml配置
settings.xml <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="h ...
- Python安装MySQLdb并连接MySQL数据库
当然了,前提是你已经安装了Python和MySQL.我的Python是2.6版本的. Python2.6的“Set”有点兼容性问题,自己照着改一下: http://sourceforge.net/fo ...
- php缓存方案
一.说说Memcached优化方案 Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提供动态.数据 ...
- java基础之数组
数组的定义 数组的应用 1, 2, 3, 4,
- jquery之onblur事件
onblur事件与html结合 function discountCheck(){ //var checkVal=$('input:text[name="Fee1"]').val( ...
- Javascript进阶篇——(DOM—认识DOM、ByName、ByTagName)—笔记整理
认识DOM文档对象模型DOM(Document Object Model)定义访问和处理HTML文档的标准方法.DOM 将HTML文档呈现为带有元素.属性和文本的树结构(节点树). 将HTML代码分解 ...
- iOS 无限轮播图的两种实现
首先说一下实现的思想: 用UIScrollView实现,在scrollView上添加3个UIImageView,分别用来显示上一张图片,当前显示的图片,下一张图片.scrollView在不滑动的时候永 ...
- String类的使用说明
(1)Length()取一个字符串的长度:public int length(); public calss StringLength1{ public static void main(String ...
- Js与flash交互:在html页面中用js与MyReport插件交互
Html页面与flash的加载 如下图,flash是html页面的一个插件节点. js与flash进行交互,首先要处理好html页面和swf的加载问题. Swf调用外部js方法,要确保js方法已存在, ...
- ORA-942 SP2-0611
环境:oracle 11.2.04 问题描述: 在使用hr用户启用set autot trace时报错 set">HR@test>set autot trace; Error O ...