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. session共享

    Nginx或者Squit反向代理到两台tomcat服务器 tomcat使用memcached tomcat连接memcached工具 cp session/*.jar /usr/local/tomca ...

  2. Oracle定时执行存储过程

    首先查看 SQL> show parameter job NAME                                 TYPE        VALUE-------------- ...

  3. Fedora21下安装cuda7.5

    Fedora21装cuda7.5 首先制作启动U盘.装好fedora21后别做任何update等yum和rpm操作,按照下面步骤走. 其中遇到用UEFI模式安装cuda时,系统一定要求提供公钥和私钥, ...

  4. openSSL命令、PKI、CA、SSL证书原理

    相关学习资料 http://baike.baidu.com/view/7615.htm?fr=aladdin http://www.ibm.com/developerworks/cn/security ...

  5. Linux数据包路由原理、Iptables/netfilter入门学习

    相关学习资料 https://www.frozentux.net/iptables-tutorial/cn/iptables-tutorial-cn-1.1.19.html http://zh.wik ...

  6. 数字证书文件格式(cer和pfx)的区别

    作为文件形式存在的证书一般有这几种格式: 1.带有私钥的证书 由Public Key Cryptography Standards #12,PKCS#12标准定义,包含了公钥和私钥的二进制格式的证书形 ...

  7. C#获取本机IP且过滤非真实网卡(如虚拟机网卡)

    参考了网上的文章,具体地址不记得了. 下面的方法可以过滤掉虚拟机的网卡等无效网卡,进而只留下真实的网卡. using System; using System.Collections.Generic; ...

  8. The C Programming Language (second edition) 实践代码(置于此以作备份)

    1. #include <stdio.h> #include <stdlib.h> #include <math.h> #include<time.h> ...

  9. polling 和 long polling 工作原理

    polling & long polling 参考:http://stackoverflow.com/questions/11077857/what-are-long-polling-webs ...

  10. WPF 窗体拖转时不触发MouseLeftButtonUpEvent

    解决方案:手动添加Handler,因为e.Handled这个属性是用在路由事件中的,当某个控件得到一个RoutedEvent,就会检测Handled是否为true,为true则忽略该事件. //手动注 ...