pow(x,n)

题目:

Implement pow(x,n)
Example 1:

Input: 2.00000, 10
Output: 1024.00000

Example 2:

Input: 2.10000, 3
Output: 9.26100

答案:

public class Solution {
    public double myPow(double x, int n) {
        if(n == 0) return 1;
        double t = 1;
        if(n < 0) {
            // Deal with integer overflow here
            if(n == Integer.MIN_VALUE) {
                n = n + 1;
                t = 1 / x;
            }
            x = 1 / x;
            n = -n;
        }
        double ret = myPow(x, n / 2);
        return t*((n % 2 == 0)? ret*ret:ret*ret*x);
    }
}

你可能感兴趣的:(pow(x,n))