我只能想出二分的方法,而且还不一定能写出最简洁的代码。无论刷多少遍,牛顿迭代法我都想不到,莫名有种悲哀的感觉:智力是硬伤啊。就算如此,却还要一遍遍不厌其烦地刷,这才是最悲剧的。多说无益,上代码。

  二分:

class Solution {
public:
int sqrt(int x) {
if(x==||x==)
return x; int left=;
int right=x;
long long mid;
long long val;
long long tmp;
while(left<right){
mid=(left+right)/;
val=mid*mid;
if(val==x)
return mid;
else if(val<x)
left=mid+;
else
right=mid-;
}
tmp=right*right;
if(tmp>x)
return right-;
else
return right;
}
};

  牛顿迭代法(java):

public class Solution {
public int sqrt(int x) {
if(x < 0) return -1; // assert(x >= 0); double n = x;
while(Math.abs(n * n - x) > 0.0001){
n = (n + x / n) / 2;
}
return (int)n;
}
}

Sqrtx的更多相关文章

  1. leetcode — sqrtx

    /** * Source : https://oj.leetcode.com/problems/sqrtx/ * * * Implement int sqrt(int x). * * Compute ...

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

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

  3. leetcode算法分类

    利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...

  4. BUG-FREE-For Dream

    一直直到bug-free.不能错任何一点. 思路不清晰:刷两天. 做错了,刷一天. 直到bug-free.高亮,标红. 185,OA(YAMAXUN)--- (1) findFirstDuplicat ...

  5. leetcode bugfree note

    463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...

  6. 转载 ACM训练计划

    leetcode代码 利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode. ...

  7. LeetCode题目分类

    利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...

  8. 69. Sqrt(x)

    题目: Implement int sqrt(int x). Compute and return the square root of x. 链接:   http://leetcode.com/pr ...

  9. <转>LeetCode 题目总结/分类

    原链接:http://blog.csdn.net/yangliuy/article/details/44514495 注:此分类仅供大概参考,没有精雕细琢.有不同意见欢迎评论~ 利用堆栈:http:/ ...

随机推荐

  1. hihoCoder 1432 : JiLi Number(吉利数)

    hihoCoder #1432 : JiLi Number(吉利数) 时间限制:1000ms 单点时限:1000ms 内存限制:256MB Description - 题目描述 Driver Ji l ...

  2. caffe + ubuntu16.04 (version without GPU)

    This Guide is based on caffe github wiki guide (https://github.com/BVLC/caffe/wiki/Ubuntu-16.04-or-1 ...

  3. 使用VC++ ATL实现iStylePDF的COM插件

    本文介绍了一种使用VC++ ATL(Active Template Library),利用ISPExtensibility接口,为 iStylePDF 加入功能简单的COM插件(addin),加入工具 ...

  4. table布局的简单网页

    ---恢复内容开始--- 由于<body>标签的图片不能够拉伸, 解决办法: 1.图片不够大,又background属性不能拉伸图片: 2.只能用个div,把其z-index值设为负,并使 ...

  5. Js Map 实现

    /* * MAP对象,实现MAP功能 * * 接口: * size() 获取MAP元素个数 * isEmpty() 判断MAP是否为空 * clear() 删除MAP所有元素 * put(key, v ...

  6. centos6.7 mutlipath install script

    #!/bin/bash if [ `id -g` != 0 ] ;then echo -e "\033[31m Please use root user\033[0m" exit ...

  7. python & c

    http://www.ibm.com/developerworks/cn/linux/l-cn-pythonandc/

  8. mysql 基础篇5(mysql语法---数据)

    6 增删改数据 -- ********一.增删改数据********* --- -- 1.1 增加数据 -- 插入所有字段.一定依次按顺序插入 INSERT INTO student VALUES(1 ...

  9. SQLServer字符操作

    1.CHARINDEX('A',‘VALUE’)    result:2 style:PATINDEX(varchar,varchar) 解释:A在字符串VALUE的位置次序. 2.PATINDEX( ...

  10. HBase 安装

    HBase 集群安装 1.上传tar包,解压tar包 tar -zxvf  hbase-1.1.5-bin.tar.gz 2.修改配置文件 进入 hbase/conf/ 在文件 regionserve ...