首页 > 编程语言 > 详细

Java for LeetCode 069 Sqrt(x)

时间:2015-05-16 20:23:09      阅读:264      评论:0      收藏:0      [点我收藏+]

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)

原文:http://www.cnblogs.com/tonyluis/p/4508453.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!