码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode Pow(x, n)

时间:2015-03-09 16:14:19      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:

Implement pow(xn).

思路:二分求,注意正负号就是了。

class Solution {
public:
    double cal(double x, int n) {
         if (n == 0) 
            return 1.0;

        double tmp = pow(x, n>>1);
        if (n & 1) 
            return tmp * tmp * x;
        else return tmp * tmp;
    }

    double pow(double x, int n) {
        if (n < 0) 
            return 1.0 / cal(x, -n);
        else return cal(x, n);
    }
};



LeetCode Pow(x, n)

标签:

原文地址:http://blog.csdn.net/u011345136/article/details/44155987

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!