leetcode之sqrt

Implement int sqrt(int x).

Compute and return the square root of x.

这道题也属于二分查找的变形

主流的方法是用牛顿插值,牛顿法的思想参考http://blog.csdn.net/StarCXDJ/article/details/18051207

将代码附在下面:

public int sqrt(int x) {
        if (x == 1)
			return 1;
		double res = x / 2;
		while (Math.abs(res * res - x) > 0.00001) {
			res = (res + x / res) / 2;
		}
		return (int) res;
    }

 最近几天开始懒惰了,不行不行!!!!坚持坚持

原文地址:https://www.cnblogs.com/gracyandjohn/p/4418453.html