首页 > 其他 > 详细

LeetCode--Sqrt(x)

时间:2015-01-12 17:40:12      阅读:325      评论:0      收藏:0      [点我收藏+]

Implement int sqrt(int x).

Compute and return the square root of x.

二分查找法:

class Solution {
public:
    int sqrt(int x) 
    {
        int high = INT_MAX;
        int low = 0;
        while(low <=high)
        {
            long long mid = (low+high)/2;
            long long temp = mid*mid;
            if(temp == x)
                return mid;
            else if(temp < x)
                low = mid+1;
            else
                high = mid-1;
        }
        return high;
    }
};

牛顿迭代法待续

LeetCode--Sqrt(x)

原文:http://blog.csdn.net/shaya118/article/details/42644395

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