leetcode[50]Pow(x, n)

Implement pow(xn).

class Solution {

public:

    double pow(double x, int n) {

        if(n==0)return 1.0;

        if(n<0)

        {

            double half=pow(x,-n/2);

            if(-n%2)return 1/(half*half*x);

            else return 1/(half*half);

        }

        return 1.0/pow(x,-n);

        if(n>0)

        {

            double half=pow(x,n/2);

            if(n%2)return half*half*x;

            else return half*half;

        }

    }

};

 

你可能感兴趣的:(LeetCode)