首页 > 其他 > 详细

leetcode No69. Sqrt(x)

时间:2016-08-04 11:41:45      阅读:257      评论:0      收藏:0      [点我收藏+]

Question:

Implement int sqrt(int x).

Compute and return the square root of x.

求平方根

Algorithm:

牛顿迭代法,技术分享迭代多次后,可以求出平方根。
还有一种雷神之锤3的程序代码,比C++标准库sqrt()的还快。
在这里贴出链接:http://blog.csdn.net/wangxiaojun911/article/details/18203333,如果有大神看懂了还麻烦教一下小白。

Accepted Code:

class Solution {  //牛顿迭代算法
public:
    int mySqrt(int x) {
        if(x<2)return x;
        int left = 0;
        int right = x;
        while(left<right)
        {
           right = (left+right)/2;
           left = x/right;
        }
        return min(left,right);
    }
};




leetcode No69. Sqrt(x)

原文:http://blog.csdn.net/u011391629/article/details/52116049

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