注意把base==0并在分母的情况考虑(临界情况需要说明),并且移位的操作比除法和取余效率要高很多。
public class Solution {
public double Power(double base, int exponent) {
if(base==0.0&&exponent<0)
return 0;
if(exponent<0){
exponent=-exponent;
base=1/base;
}
return unsignedResult(base,exponent);
}
public double unsignedResult(double base,int exponent){
if(exponent==0)
return 1;
if(exponent==1)
return base;
double result=unsignedResult(base,exponent>>1);
result*=result;
if((exponent&1)!=0)
result*=base;
return result;
}
}