Monday, April 27, 2015

[LeetCode] Pow(x, n)

Implement pow(x, n).

class Solution {
public:
    double myPow(double x, int n) 
    {
        if (n==0) return 1;
        double t = pow(x,n/2);
        if (n%2) 
        {
            return n<0 ? 1/x*t*t : x*t*t;
        } 
        else 
        {
            return t*t;
        }
    }
};