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

  二分:

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. iframe在浏览器中session失效问题

    iis中右击项目属性http头 添加一个http头 X-UA-Compatible 自定义http头值 IE=EmulateIE7 这样设置后就可以了

  2. php : 基础(4)

    流程控制 循环结构 循环的中断 循环中,有两种中断语句可以使用: break: 用于完全终止某个循环,让执行流程进入到循环语句后面的语句: continue: 用于停止当前正在进行的当次循环,而进入到 ...

  3. c#_1:后台post请求

    1:aspx内容 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Push.as ...

  4. iOS runtime 初步学习

    注: 在Xocde5之后, 使用运行时方法需要进行2步设置1. 在Build Setting中搜索'msg', 设置'Strict Checking' 为 NO2. 使用需要导入头文件 #import ...

  5. Java学习--内部类(一)

    Java学习--内部类(一) 一. 内部类的定义和特点 class Outer{ privite int num = 5; class Inner{ public void Display(){ Sy ...

  6. MFC编程入门之二十六(常用控件:滚动条控件ScrollBar)

    回顾上一节,讲的是组合框控件Combo Box的使用.本节详解滚动条控件Scroll Bar的相关内容. 滚动条控件简介 滚动条大家也很熟悉了,Windows窗口中很多都有滚动条.前面讲的列表框和组合 ...

  7. ajax 和 post 传多个参数值具体怎么写

    ajax data:{id:id,name:name} 正确 data:{id:"001",name:"王俊凯"} 正确 data:{"id" ...

  8. git clone --early EOF

    出现这个问题可能需要重新检查以下方面: 1. Android studio Git 的安装地址:  ..../Git/cmd/git.exe 记得在环境变量 --Path 中进行配置: ,..../G ...

  9. MySQL DDL 整理

    DDL is Data Definition Language statements. Some examples:数据定义语言,用于定义和管理 SQL 数据库中的所有对象的语言 -- 清空表内容 T ...

  10. web api+递归树型结构

    using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Ne ...