首页 > 其他 > 详细

[LeetCode]73. Sqrt(x)平方根

时间:2015-11-11 22:13:37      阅读:382      评论:0      收藏:0      [点我收藏+]

Implement int sqrt(int x).

Compute and return the square root of x.

 

Subscribe to see which companies asked this question

 

解法1:使用求正数的平方根的一般二分查找法:

class Solution {
public:
    int mySqrt(int x) {
        if(x == 0 || x == 1) return x;
        double limit = 0.000001;
        double left = 0, right = x, mid = (left + right) / 2.0;
        double dist = x - mid * mid;
        while (abs(dist) > limit) {            
            if (dist > limit) left = mid;
            else right = mid;
            mid = (left + right) / 2.0;
            dist = x - mid * mid;
        }
        return mid;
    }
};

 

解法2:因为输出为int型,实际并不需要精确求出x的实际平方根,因此可以改进:

 

class Solution {
public:
    int mySqrt(int x) {
        long long left = 0, right = x;
        while (left <= right) {
            long long mid = (left + right) / 2;
            long long sqrt = mid * mid;
            if (sqrt == x) return mid;
            else if (sqrt > x) right = mid - 1;
            else left = mid + 1;
        }
        return right; //注意若无精确值,则返回略小于其平方根的那个
    }
};

 

解法3:可以使用牛顿迭代法求f(x)=0的根,而求n的平方根相当于求解方程f(x)=x^2-n=0的根,所以可以使用此方法。

设r是技术分享 的根,选取技术分享 作为r的初始近似值,过点 技术分享 做曲线 技术分享 的切线L,L的方程为 技术分享 ,求出L与x轴交点的横坐标 技术分享 ,称x1为r的一次近似值。过点 技术分享 做曲线 技术分享 的切线,并求该切线与x轴交点的横坐标 技术分享 ,称 技术分享 为r的二次近似值。重复以上过程,得r的近似值序列,其中, 技术分享 称为r的 技术分享 次近似值。

将上面公式应用到方程f(x)=x^2-n=0即可得到公式:

技术分享

一般取初始值x0=1。

class Solution {
public:
    int mySqrt(int x) {
        double limit = 0.000001, root = 1.0;
        while (abs(x - root * root) > limit)
            root = (root + x / root) / 2.0;
        return root;
    }
};

 

[LeetCode]73. Sqrt(x)平方根

原文:http://www.cnblogs.com/aprilcheny/p/4957370.html

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