pow(x, n)

class Solution {
public:
    double myPow(double x, int n) {

        if(n < 0)
        {
            if(n == INT_MIN)
            {
                /*
                int tmp = INT_MIN/2;
                return 1.0 / power(x, -tmp) * power(x, -tmp);
                */
                if(x == 1 || x == -1)
                {
                    return 1.0;
                }

                return 0.0/1.0;
            }
            return 1.0 / power(x, -n);
        }
        else
        {
            return power(x, n);
        }
    }

    double power(double x, int n)
    {
        if(n == 0)
        {
            return 1;
        }

        double v = myPow(x, n/2);

        if(n%2 == 0)
        {
            return  v*v;
        }
        else
        {
            return v*v*x;
        }
    }
};

results matching ""

    No results matching ""