参考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. javascript事件与event对象的属性

    javascript事件列表解说 事件 浏览器支持 解说 一般事件 onclick IE3.N2 鼠标点击时触发此事件 ondblclick IE4.N4 鼠标双击时触发此事件 onmousedown ...

  2. MVC - 11(下)jquery.tmpl.js +ajax分页

    继续 mvc-11(上).dto:http://www.cnblogs.com/tangge/p/3840060.html jquery.tmpl.js 下载:http://pan.baidu.com ...

  3. ASP.NET Web Api 使用CacheCow和ETag缓存资源(转载)

    转载地址:http://www.cnblogs.com/fzrain/p/3618887.html 前言 本文将使用一个开源框架CacheCow来实现针对Http请求资源缓存,本文主要介绍服务器端的缓 ...

  4. SQLAchemy Core学习之Reflection

    如果以后万一有一个定义好了的库,可以用这种反射的方法,作常用的操作. #coding=utf-8 from datetime import datetime from sqlalchemy impor ...

  5. Acdream 1111:LSS(水题,字符串处理)

    LSS Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 128000/64000 KB (Java/Others) SubmitStati ...

  6. Intent传递对象的两种方法(Serializable,Parcelable) (转)

    今天讲一下Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object);另一种是Bundle.putParcela ...

  7. HorizontalScrollView

    HorizontalScrollView 链接

  8. java中如何实现类似goto的作法

    goto虽然是java中保留的keyword,但是对于跳转这个语法对新手来说这个确实好用.为了提高程序的可靠性和可读性,Java语言目前是不支持无条件跳转的goto语句!! 幸亏java中有高仿跳转的 ...

  9. C# break continue return

    break:跳出当前循环,执行循环后的代码 continue:跳出当前循环,执行下一次循环 return:跳出整个方法

  10. react-redux(2)

    中间件 机制: 建立一个store.dispatch的链条,每个middleware是链条中的一个环节,传入的action对象逐步处理,直到最后出来是Javascript Plain Object; ...