Medium
n为负数的时候,取n= -(n + 1)因为怕遇到Integer.MIN_VALUE取负出现溢出
class Solution {
public double myPow(double x, int n){
if (x == 0){
return 0;
}
if (n == 0 || x == 1){
return 1;
}
if (n < 0){
//Integer.MIN_VALUE
n++;
n = -n;
x = 1/x;
return x*myPow(x, n);
}
if (n % 2 == 0){
return myPow(x*x, n/2);
} else {
return x*myPow(x*x, n/2);
}
}
}