Implement int sqrt(int x).

Compute and return the square root of x.

解题思路一:

    public int mySqrt(int x) {
return (int)Math.sqrt(x);
}

神奇般的Accepted。

解题思路二:

参考平方根计算方法 计算平方根的算法

这里给出最简单的牛顿法,JAVA实现如下:

    public int mySqrt(int x) {
double g = x;
while (Math.abs(g * g - x) > 0.000001)
g = (g + x / g) / 2;
return (int) g;
}

Java for LeetCode 069 Sqrt(x)的更多相关文章

  1. LeetCode 069 Sqrt(x) 求平方根

    Implement int sqrt(int x).Compute and return the square root of x.x is guaranteed to be a non-negati ...

  2. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  3. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  4. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  5. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  6. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  7. Java for LeetCode 200 Number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  8. Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  9. Java for LeetCode 154 Find Minimum in Rotated Sorted Array II

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

随机推荐

  1. 【POJ 1260】Pearls

    题 题意 有n个(n≤100)等级的珍珠,等级越高单价越高,要购买一种等级的珍珠就要多付10*单价,现在需要购买一些等级的珍珠一定数量,若买更高等级的珍珠更便宜则可以买更高等级的珍珠,求最少花费. 分 ...

  2. oracle存储过程执行中输出日志文件

    create or replace procedure p_outputdebug(a varchar2,b varchar2,c varchar2)is vFileName varchar2(100 ...

  3. SOM自组织映射网络 教程

    概述 SOM是芬兰教授Teuvo Kohonen提出的一种神经网络算法,它提供一种将高维数据在低维空间进行表示的方法(通常是一维或二维).缩减向量维度的过程,叫做向量量化(vector quantis ...

  4. HDU #3333

    Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Descript ...

  5. linux c学习笔记----进程创建(fork,wait,waitpid)

    1.pid_t fork(); (1)当一个进程调用了fork 以后,系统会创建一个子进程.这个子进程和父进程不同的地方只有他的进程ID 和父进程ID,其他的都是一样.就象符进程克隆(clone)自己 ...

  6. Spring学习4-面向切面(AOP)之schema配置方式

    一.通过Scheme配置实现AOP步骤(Spring AOP环境的环境与上篇博文 Spring接口方式相同)    步骤一.编写业务类: public class AspectBusiness {   ...

  7. Activator.CreateInstance 反射实例化对象

    public class CommonReq { private String TransNo { get; set;} public String SubmitData { get; set; } ...

  8. 锋利的jQuery-2--一个显示和隐藏的例子,主要看写法

    例子:如图,默认不显示全部,点击按钮来回切换,全部显示是一部分推荐的品牌高亮. $(function(){ //dom加载完再执行 var category = $('ul li:gt(5):not( ...

  9. 记录一次centos6.4版本的VSFTP本地用户登陆的配置

    其实vsftp是一个非常常用而且简单的服务,但是假如服务不是你配置的前者没有留下参考档案,的确是件头疼的事儿,特此记录下. 首先是vsftp的安装当然安装有源码的编译和yum等 这里我选择rpm包的y ...

  10. MYSQL例题合集

    一.数学函数 数学函数主要用于处理数字,包括整型.浮点数等. ABS(x) 返回x的绝对值 SELECT ABS(-1) -- 返回1 CEIL(x),CEILING(x) 返回大于或等于x的最小整数 ...