Sqrt(x) - LintCode
examination questions
Implement int sqrt(int x)
.
Compute and return the square root of x.
sqrt(3) = 1
sqrt(4) = 2
sqrt(5) = 2
sqrt(10) = 3
O(log(x))
解题代码
class Solution {
public:
/**
* @param x: An integer
* @return: The sqrt of x
*/
int sqrt(int x) { if (x == ){
return ;
} long long int temp = x / ;
int deposit;
while (true){
if (temp * temp > x){
deposit = temp;
temp = temp / ;
}
else{
if ((temp + )*(temp + ) > x){
return temp;
}
else{
temp = (deposit + temp) / ;
}
}
}
}
};
算法分析
使用二分法,给定一个要求的数,然后分半,若该数的平方大于给定的数,则对该数进行平分,如果平分后的平方小于给定的数,那么取该数与平分前的数的中数进行平方,每一次平方后如果该数是大于给定的数的,那么检测与该数小1的数的平方是否小于给定的数,如果是,那么该数为结果。
代码解析
class Solution {
public:
/**
* @param x: An integer
* @return: The sqrt of x
*/
int sqrt(int x) { if (x == ){ //如果为1,那么直接得出结果
return ;
} long long int temp = x / ; //二分
int deposit; //用于二分前的值,存放
while (true){
if (temp * temp > x){ //大于就二分
deposit = temp; //存放
temp = temp / ; //二分
}
else{
if ((temp + )*(temp + ) > x){ //如果+1的平方大于x的话,那么就是该值
return temp;
}
else{
temp = (deposit + temp) / ; //二分前与二分后的中值
}
}
}
}
};
Sqrt(x) - LintCode的更多相关文章
- LintCode Sqrt(x)
Implement int sqrt(int x). Compute and return the square root of x. Have you met this question in a ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)
--------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...
- leetcode & lintcode for bug-free
刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...
- LintCode题解之判断是否为平方数之和
简单粗暴 public class Solution { /* * @param : the given number * @return: whether whether there're two ...
- leetcode & lintcode 题解
刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...
- 速算1/Sqrt(x)背后的数学原理
概述 平方根倒数速算法,是用于快速计算1/Sqrt(x)的值的一种算法,在这里x需取符合IEEE 754标准格式的32位正浮点数.让我们先来看这段代码: float Q_rsqrt( float nu ...
- [LeetCode] Sqrt(x) 求平方根
Implement int sqrt(int x). Compute and return the square root of x. 这道题要求我们求平方根,我们能想到的方法就是算一个候选值的平方, ...
- Leetcode 69. Sqrt(x)
Implement int sqrt(int x). 思路: Binary Search class Solution(object): def mySqrt(self, x): "&quo ...
随机推荐
- 【转】NumPy-快速处理数据
2.0 简介 标准安装的Python中用列表(list)保存一组值,可以用来当作数组使用,不过由于列表的元素可以是任何对象,因此列表中所保存的是对象的指针(为了保存各种类型的对象,只能牺牲空间).这样 ...
- IOS第18天(3,CALayer隐式动画)
******隐式动画(手指拖拽Layer) #import "HMViewController.h" @interface HMViewController () @propert ...
- Code Snippet
Code Snippet: http://msdn.microsoft.com/en-us/library/z41h7fat.aspx CodePlex.Snippets 4.0 - Visual S ...
- Thinking in Java——笔记(10)
Inner Classes It allows you to group classes that logically belong together and to control the visib ...
- switch多分支语句
1.switch多分支语句的语法 switch(表达式){ case 常量值:要执行的语句; break; case 常量值:要执行的语句; break; case 常量值:要执行的语句; break ...
- XE5 ImageList的BUG?
今天做界面, 在imagelist里加载一个带有半透明通道的PNG图, 结果发现图片居然发暗, 如下: 原图: IDE里加载以后的图: 明显变暗...查询了源码, 无果 然后又用2010去测试, 发现 ...
- mysql重点--执行计划
explain SQL: 在sql语句前面加explain实现"执行计划"的功能.功能是比较准确的显示将要执行这条sql语句的运行状况. select_simple 是查询类型:t ...
- 《Android 性能测试初探》
移动测试站点推荐: https://testerhome.com/ 专项相关帖子推荐: <Android 性能测试初探>合集 移动无线应用专项测试浅谈 公开课: [腾讯课堂]Testerh ...
- Python开发【第五章】:Python常用模块
一.模块介绍: 1.模块定义 用来从逻辑上组织python代码(变量,函数,类,逻辑:实现一个功能),本质上就是.py结尾python文件 分类:内置模块.开源模块.自定义模块 2.导入模块 本质:导 ...
- ORA-25153: Temporary Tablespace is Empty
在一测试环境做测试的时候发生以下错误: SQL> exec dbms_stats.gather_table_stats(ownname=>'SYS',tabname=>'TT',me ...