注意 Integer.MIN_VALUE取负 溢出的情况 所以加入了long data type;
代码:
public class Solution {
public double myPow(double x, int n) {
int flag = 0;
if(n == 0) return 1.0;
long times = (long) n;
if(n < 0){
times = 0 - times;
flag = 1;
}
double result = binaryPow(x, times);
if(flag == 1) return 1/result;
return result;
}
public double binaryPow(double x, long n){
if(n == 1) return x;
double half = binaryPow(x, n>>1);
if(n%2 != 0) return half*half*x;
return half * half;
}
}