Leetcode 50 Pow(x, n)
6.7 简单题,分治
public class Solution {
public static void main(String[] args){
double x = 2.0000;
int n = -2147483648;
Solution s = new Solution();
s.myPow(x,n);
}
public double myPow(double x, int n) {
if(x==1||n==0) return 1;
if(x==0) return 0;
if(n==1) return x;
if(n==2) return x*x;
double temp = myPow(x,n/2);
if(n>0){
if(n%2==0) return temp*temp;
else return temp*temp*x;
}else{
x = 1/x;
if(n==Integer.MIN_VALUE){
return myPow(x, Integer.MAX_VALUE)*x;
}
return myPow(x,-n);
}
}
}