参考Babylonian method

 (x0  越接近S的平方根越好)

class Solution {
public:
int sqrt(double x) {
if(x == ) return ;
double root = x/, tolerance = 1.0e-2;
do{
root=(root+x/root)/;
}while(abs(root*root-x)>tolerance);
return root;
}
};

这题感觉题目有问题,返回的平方根竟然是整数,

另一种方法是是用二分搜索

class Solution {
public:
int sqrt(int x) {
if(x < ) return x;
int left = , right = x;
while(left <= right){
int mid = (right+left)/;
if(mid < x/mid) left = mid+;
else if(x/mid < mid ) right = mid-;
else return mid;
}
return right;
}
};

如果题目要求的时浮点数可以考虑利用浮点数二分搜索

Leetcode Sqrt(x)的更多相关文章

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

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

  2. [leetcode]Sqrt(x) @ Python

    原题地址:https://oj.leetcode.com/problems/sqrtx/ 题意: Implement int sqrt(int x). Compute and return the s ...

  3. LeetCode: Sqrt

    Title: Implement int sqrt(int x). Compute and return the square root of x. 思路:这个平方根肯定是在[1,x]之间,所以在这个 ...

  4. leetcode—sqrt

    1.题目描述   Implement int sqrt(int x).   Compute and return the square root of x. 2.解法分析 很明显,用二分搜索可解,但是 ...

  5. LeetCode:Sqrt(x) 解题报告

    Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. SOLUTION 1: 参见:二分法总结,以及模 ...

  6. LeetCode——Sqrt(x)

    Description: Implement int sqrt(int x). Compute and return the square root of x. 好好学习数学还是非常有用的,牛顿迭代法 ...

  7. [Leetcode] sqrt 开根号

    Implementint sqrt(int x). Compute and return the square root of x. 题意:求根号下x 的值 思路:使用二分搜索.先定义x平方根的取值区 ...

  8. [LeetCode] Sqrt(x) 二分搜索

    Implement int sqrt(int x). Compute and return the square root of x. Hide Tags Math Binary Search     ...

  9. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

随机推荐

  1. md5sum

    [root@NB index]# ls index()().html index()().html index()().html index()().html index()().html [root ...

  2. memarch

    memached 是一个高性能的分布式对象缓存系统,用于动态web应用以减轻数库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态数据驱动网站的速度.memcached基于一个存储 ...

  3. mysql 查看用户的权限

    show grants for 'username'@'%';

  4. 数据结构和算法 – 8.链表

    8.1.数组存在的问题 在处理列表的时候数组是常用的数据结构.数组可以对所存储的数据项提供快速地存取访问,而且它很易于进行循环遍历操作.当然,数组已经是语言的一部分了,用户不需要使用额外的内存,也不需 ...

  5. GBDT原理实例演示 1

    考虑一个简单的例子来演示GBDT算法原理 下面是一个二分类问题,1表示可以考虑的相亲对象,0表示不考虑的相亲对象 特征维度有3个维度,分别对象 身高,金钱,颜值     cat dating.txt ...

  6. Ubuntu下快速安装php环境

    今天蛋疼了一下,在Ubuntu下装了一下php的环境,也就是装了一下MySQL.PHP.Apache.话说还真是简单...不禁让我想起原来在windows下开发的时候撑死就是装不上,而且一个就是几个G ...

  7. Sonar规则学习笔记

    1. A catch statement should never catch throwable since it includes errors. 在catch里永远不要抛出throwable. ...

  8. Effective C++ 之 Item 4:确定对象被使用前已先被初始化

    Effective C++ Chapter 1. 让自己习惯C++ (Accustoming Yourself to C++) Item 4. 确定对象被使用前已先被初始化 (Make sure th ...

  9. 在Salesforce中实现对Object的增删改查操作

    1): Insert Statement    http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#CSHID=ap ...

  10. 函数调用关于从Ring3转到Ring0 ESP堆栈变化

    在ring0堆栈获取ring3堆栈方式 第一种方式 [esp+4] == [esp+参数个数*4+4] 如果这里不相等就需要用第二种方式 [[esp+参数个数*4+8]] 这里面的值就是Ring3的堆 ...