Skip to content

Latest commit

 

History

History
23 lines (20 loc) · 445 Bytes

050.md

File metadata and controls

23 lines (20 loc) · 445 Bytes

50. Pow(x, n)

Solution 1 (O(log(n)))

class Solution {
    public double myPow(double x, int n) {
        long n_long = n;
        return _pow(x, n);
    }

    private double _pow(double x, long n) {
        if (n == 0)
            return 1;
        if (n < 0)
            return 1.0 / _pow(x, -n);
        if (n % 2 == 0)
            return _pow(x * x, n / 2);
        else
            return _pow(x * x, n / 2) * x;
    }
}