leetcode[50]Pow(x, n)

2131 ワード

Implement pow(x, n).
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;

        }

    }

};