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. javaEE规范和SSH三大框架到底有什么关系

    转自博客:http://blog.csdn.net/bingjing12345/article/details/20641891 1994-2000 年是互联网的大航海时代. 请注意,下面的时间点及其 ...

  2. java系统高并发解决方案(转载)

    转载博客地址:http://blog.csdn.net/zxl333/article/details/8454319 转载博客地址:http://blog.csdn.net/zxl333/articl ...

  3. 友盟iOS推送配置(从真机调试到推送)

    下面我来讲解一下友盟iOS的推送配置,其实友盟只是一个示例,换做其余的第三方推送服务也会适用,只是第三方的后面服务变了而已. iOS推送(包括真机调试)所需要的步骤和文件如下: 备注:这里我将省略掉一 ...

  4. POJ3264 Balanced Lineup

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 44720   Accepted: 20995 ...

  5. 802.11协议帧格式、Wi-Fi连接交互过程、无线破解入门研究

    相关学习资料 Linux黑客大曝光: 第8章 无线网络 无线网络安全攻防实战进阶 无线网络安全 黑客大曝光 第2版 http://zh.wikipedia.org/wiki/IEEE_802.11 h ...

  6. schemaLocation value = 'xxxxxxxxxxxx' must have even number of URI's

    这是因为没有加上Spring的版本号,加上就行了,如: http://www.springframework.org/schema/beans/spring-beans.xsd -3.2.2 http ...

  7. Linux命令详解:[7]获得命令帮助

    在维护和使用Linux系统时,常常会忘记命令的使用方法,如果旁边又没有相应的资料,那怎么办呢?不用担心,系统本身提供了详细的手册供使用者查询,下面小编就以CentOS6.4系统为例演示如何获得命令帮助 ...

  8. 在tp中使用mongo数据库并建立连接的实例

  9. WINDOWS渗透与提权总结(1)

    旁站路径问题: 1.读网站配置. 2.用以下VBS: 01 On Error Resume Next 02   03 If (LCase(Right(WScript.Fullname, 11)) = ...

  10. mysql 视图(view)

    什么是视图 视图是从一个或多个表中导出来的表,是一种虚拟存在的表. 视图就像一个窗口,通过这个窗口可以看到系统专门提供的数据. 这样,用户可以不用看到整个数据库中的数据,而之关心对自己有用的数据. 数 ...