public class Solution {
public double pow(double x, int n) {
if(n == 0)
return 1;
if(n<0){
n = n;
x = 1/x;
}
return (n%2 == 0) ? pow(x*x, n/2) : x*pow(x*x, n/2);
}
}
Short and easy to understand solution

Similar idea (no overflow check): decreasing n by dividing by 2. After we get the return result, multiply by itself. If n is a odd number, multiply one x if n is position; otherwise, 1 / x.
public class Solution { public double myPow(double x, int n) { if(n == 0) return 1.; double res = myPow(x, n / 2); return n % 2 == 0 ? res * res : n < 0 ? res * res * (1 / x) : res * res * x; } }