Share my clean recursive solution


  • 2
    I
    public class Solution {
        public double pow(double x, int n) {
            return n > 0 ? power(x, n) : 1 / power(x, -n);
        }
        double power(double x, int n) {
            if (n == 1) return x;
            if (n == 0) return 1.0;
            double tmp = power(x, n / 2);
            return n % 2 == 0 ? tmp * tmp : tmp * tmp * x;
        }
    }

Log in to reply
 

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