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. 如何获得中国所有的IP地址段

    转自: https://wjianz.wordpress.com/2014/09/12/howto-%E4%BB%8Eapnic%E8%8E%B7%E5%8F%96%E4%B8%AD%E5%9B%BD ...

  2. Ueditor 1.4.3.1 使用 ThinkPHP 3.2.3 的上传类进行图片上传

    在 ThinkPHP 3.2.3 中集成百度编辑器最新版 Ueditor 1.4.3.1,同时将编辑器自带的上传类替换成 ThinkPHP 3.2.3 中的上传类. ① 下载编辑器(下载地址:http ...

  3. 利用Sonar规则结合WebStorm进行Code Inspect

    1.目的 在编写代码时会受到公司Sonar规则的限制,不想在编写完成后再对代码进行Inspect,回头再来一个个修正,费时费力. 那么,下面将通过优秀的WebStorm开发工具自身的CodeInspe ...

  4. JQuery-筛选

    /* 六丶筛选 */ // console.log( $("div:first") ); // div#id_1 所有div中的第一个 // console.log( $(&quo ...

  5. 【Android测试】【第十七节】Instrumentation——App任你摆布(反射技术的引入)

    ◆版权声明:本文出自胖喵~的博客,转载必须注明出处.  转载请注明出处:http://www.cnblogs.com/by-dream/p/5569844.html 前言 学习了上节之后,大家是否已经 ...

  6. Maven-004-使用 Nexus 搭建 maven 私服

    从去年至今,自己一直在学习自动化测试工具,想利用自动化工具尽可能的将重复的.关键的.耗时耗力的工作实现自动化,减轻日常测试工作,提升测试效率.在学习的过程中,将 maven 作为了项目开发管理工具,进 ...

  7. wpa supplicant 移植

    最近移植wifi,WIFI芯片使用rtl8723.在文件系统生成了设备节点.需要移植工具进行测试: iwconfig:没有密码的或者wep加密的wifi,使用iwconfig就已经够用. wpa_su ...

  8. zjuoj 3608 Signal Detection

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3608 Signal Detection Time Limit: 2 Sec ...

  9. POJ - 2339 Rock, Scissors, Paper

    初看题目时就发了个错误,我因为没有耐心看题而不了解题目本身的意思,找不到做题的突破口,即使看了一些题解,还是没有想到方法. 后来在去问安叔,安叔一语道破天机,问我有没有搞清题目的意思,我才恍然大悟,做 ...

  10. PHP composer 安装流程

    打开php的openssl扩展 可以在wamp管理器里直接打开openssl扩展,也可以使用php命令打开扩展,还可以通过修改php.ini后重启打开扩展. 下面给出命令打开扩展的示例: 可以通过以下 ...