examination questions

Implement int sqrt(int x).

Compute and return the square root of x.

Example

sqrt(3) = 1

sqrt(4) = 2

sqrt(5) = 2

sqrt(10) = 3

Challenge

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的更多相关文章

  1. LintCode Sqrt(x)

    Implement int sqrt(int x). Compute and return the square root of x. Have you met this question in a ...

  2. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  3. (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)

    --------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...

  4. leetcode & lintcode for bug-free

    刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...

  5. LintCode题解之判断是否为平方数之和

    简单粗暴 public class Solution { /* * @param : the given number * @return: whether whether there're two ...

  6. leetcode & lintcode 题解

    刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...

  7. 速算1/Sqrt(x)背后的数学原理

    概述 平方根倒数速算法,是用于快速计算1/Sqrt(x)的值的一种算法,在这里x需取符合IEEE 754标准格式的32位正浮点数.让我们先来看这段代码: float Q_rsqrt( float nu ...

  8. [LeetCode] Sqrt(x) 求平方根

    Implement int sqrt(int x). Compute and return the square root of x. 这道题要求我们求平方根,我们能想到的方法就是算一个候选值的平方, ...

  9. Leetcode 69. Sqrt(x)

    Implement int sqrt(int x). 思路: Binary Search class Solution(object): def mySqrt(self, x): "&quo ...

随机推荐

  1. 【转】java开源类库pinyin4j的使用

    最近CMS系统为了增加查询的匹配率,需要增加拼音检索字段,在网上找到了pinyin4j的java开源类库,提供中文转汉语拼音(并且支持多音字), 呵呵,看了看他的demo,决定就用它了,因为我在实际使 ...

  2. 【iCore3 双核心板】例程十九:USBD_MSC实验——虚拟U盘

    实验指导书及代码包下载: http://pan.baidu.com/s/1i4eNbQd iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...

  3. CentOS7安装配置redis-3.0.0

    一.安装必要包 yum install gcc 二.linux下安装 #下载 wget http://download.redis.io/releases/redis-3.0.0.tar.gz tar ...

  4. windows的Timer和写文件方式串口注意!

    1.Timer要读取并分发消息,才能触发自定义回调函数 SetTimer(NULL, 1, 40, (TIMERPROC)TimerProc); while(GetMessage(&msg, ...

  5. LeetCode Strobogrammatic Number

    原题链接在这里:https://leetcode.com/problems/strobogrammatic-number/ 题目: A strobogrammatic number is a numb ...

  6. RDIFramework.NET ━ .NET快速信息化系统开发框架 V2.8 版本发布

    (新年巨献) RDIFramework.NET ━ .NET快速信息化系统开发框架 V2.8 版本发布 历时数月,RDIFramework.NET V2.8版本发布了,感谢大家的支持. RDIFram ...

  7. C++复现经典游戏——扫雷

    国庆小长假,当大家都去看人山人海的时候,我独自一人狂码代码.这两天想要实现的内容是Windows上的一个经典游戏——扫雷.相信90后和一些上班族对此并不陌生.然而,从win8开始,扫雷就不再是Wind ...

  8. ubuntu 15.04 联网提示:wifi已通过硬件开关禁用

    执行以下命令: echo "options asus_nb_wmi wapf=4"| sudo tee /etc/modprobe.d/asus_nb_wmi.conf 重启

  9. C++之路进阶——codevs2451(互不侵犯)

    2451 互不侵犯 2005年省队选拔赛四川  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master       题目描述 Description 在N×N的棋盘里 ...

  10. datatable动态添加,及填充数据

    DataTable tblDatas = new DataTable("Datas"); tblDatas.Columns.Add("ID", Type.Get ...