leetcode-Pow(x, n)

class Solution {
public:
    double myPow(double x, int n) {
        if(n==0)  
            return 1.0;
        if(n==1)
            return x;
        if(n<0){
            x=1/x;
            n=-n; 
        }               
        double half = myPow(x,n>>1);  
        if(n&1)  
            return half*half*x;  
        else  
            return half*half;  
    }
};

你可能感兴趣的:(leetcode-Pow(x, n))