首页 > 其他 > 详细

LeetCode---Pow(x, n)

时间:2015-01-12 13:05:41      阅读:174      评论:0      收藏:0      [点我收藏+]

Implement pow(xn).

class Solution 
{
public:
    double pow(double x, int n) 
    {
        if(x == 1)
            return 1;
        if(x == -1)
        {
            if(n%2 == 0)
                return 1;
            return -1;
        }
        if(n<0)
            return 1/pow(x,-n);
        double res=1;
        double count = x;
        while(n>0)
        {
            int flag = n&1;
            if(flag == 1)
                res = res*x;
            x = x*x;
            n >>=1;
        }
        return res;
    }
};


LeetCode---Pow(x, n)

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

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