LeetCode - Pow(x, n)

Question Definition

Implement pow(x, n).

Example 1:

Input: 2.00000, 10
Output: 1024.00000

Example 2:

Input: 2.10000, 3
Output: 9.26100

Java Solution

public double myPow(double x, long n) {
    if(x == 1 || n == 0) return 1;
    if(n < 0) return 1 / myPow(x, -n);
    if(n == 1) return x;
    if(n % 2 == 0) {
        double half = myPow(x, n / 2);
        return half * half;
    }
    return myPow(x, n - 1) * x;
}

Comments