Can't be more concise


  • 1
    S
    double pow(double x, int n) {
        if (x == 0) return 0;
        if (n == 0) return 1;
        if (n < 0) n = -n, x = 1/x;
        return n % 2 ? x*pow(x*x, n/2) : pow(x*x, n/2); 
    }

  • -1
    B

    Yes, it can be. If you can come up with a O(1) memory solution (is there is one). You are using O(lg(n)) system stack.
    May be it will be a couple of more lines in your code, but better complexity beats smaller code any day.
    Just encouraging...


  • 0
    E

    He said concise not efficient


Log in to reply
 

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