My nice C++ solution


  • 0
    Z
    class Solution {
    public:
        double pow(double x, int n) {
            if (n == 0) return 1;
            if (abs(n) % 2 == 1) {
                if (n < 0) return 1/(x*pow(x*x, (-(n+1))/2));
                else return x * pow(x * x, n / 2);
            } else {
                if (n < 0) return 1/(x*x*pow(x*x, (-(n+2))/2));
                else return pow(x * x, n / 2);
            }
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.