Implement pow(x, n).
public class Solution {
public double pow(double x, int n) {
//判断x是不是0
if(Math.abs(x-0)<0.0000001)
return 1;
//指数为负数
if(n<0){
return 1/p(x,-((long)n));
}
if(n==0)
return 1;
return p(x,n); } public double p(double x,long n){
if(n==0)
return 1;
if(n==1)
return x;
if(n%2==0){
double r=p(x,n/2);
return r*r;
}
double r=p(x,(n-1)/2);
return r*r*x;
}
}