[leetcode]Pow(x, n)

class Solution {
public:
    double powPositive(double x, int n){
        if(n == 0) return 1;
        if(n == 1) return x;
        
        double tmp;
        if(n%2 == 0){
            tmp = powPositive(x, n/2);
            return tmp*tmp;
        }
        
        tmp = powPositive(x, n/2);
        
        return tmp*tmp*x;
    }
    
    double pow(double x, int n) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if(n >= 0) return powPositive(x,n);
        return 1/powPositive(x,-n);
        
    }
};


posted @ 2013-08-02 22:55  javawebsoa  Views(214)  Comments(0Edit  收藏  举报