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

  二分:

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. OpenCV2+入门系列(二):图像的打开、创建与显示(命令行)

    前置知识:数字图像的简略知识 这里只是最基础的知识,上课如果稍微听了课的同学可以直接略过不不看. 彩色图像: 对于一副数字图像,对于一副RGB色彩空间的彩色数字图像,它一共有宽X高个像素格子,每个格子 ...

  2. Java - 安全的退出线程

    stop() 存在的问题 使用 stop() 来退出线程是不安全的.它会解除由线程获取的所有锁,可能导致数据不一致. 举个例子: public class StopTest { public stat ...

  3. 复利计算器(4)——jQuery界面美化、自动补全

    一.分工 这次终于可以跟小伙伴合作了,经过讨论,我负责界面的美化和输入框自动补全,小伙伴擅长安卓,于是将复利计算器弄成app的任务就交给了小伙伴.为了我们两人团队,我们都好奋斗哈哈哈!! 二.界面美化 ...

  4. 集​群​t​o​m​c​a​t​+​a​p​a​c​h​e​配​置​文​档

    http://wenku.baidu.com/link?url=M_Lt07e-9KTIHucYgJUCNSxkjWThUuQ2P8axn8q6YmY_yQw7NmijQoDA2wKmi_FQUxwO ...

  5. About vector

    今天打vector又打炸了不!高!兴! vecotr头文件 #include<vector> 定义域 using namespace std; 或using std::vector; 初始 ...

  6. 基础笔记3(二)(专门处理String的正则表达式)

    1.常规判断一个字符串是以什么开头,是否是数字的判断方式有: a.通过比较每个字符,注意比较是字符值(ASc码值),不是字面值 String s="); //判断每个字符数组的每个字符 ch ...

  7. ubuntu 编译源码坏境配置

    git checkout -b newlocal origin/q01v31source build/envsetup.shlunch msm8916_32-usermake -j4 make -j4 ...

  8. node与socket.io搭配小例子-转载

    //服务端代码 io = require('socket.io').listen(app), fs = require('fs'), cookie=require('cookie'); request ...

  9. spring-boot 之 使用Admin监控应用

    https://yq.aliyun.com/articles/2322 ************************************* 摘要: Spring Boot提供的监控接口,例如: ...

  10. MVC项目中使用js 设置Checkbox的选中事件

    要实现的效果是,当点击checkbox时,跳转到Action中 CheckBox实例: View界面: @Html.CheckBox("prd.IsChecked", Model. ...