### 常规解法

``````public class Solution {
public double Power(double base, int exponent) throws Exception {
if(exponent == 0){
if(base == 0){
throw new Exception();
}else{
return 1;
}
}

if(exponent == 1){
return base;
}

double result = base;

double flag = exponent > 0 ? 1 : -1;
exponent -= flag;
while(exponent != 0){
result = result * base;
exponent -= flag;
}
return flag == 1 ? result : (1 / result);
}
}
``````

### 转换为斐波那契数列，使用递归解决

``````public class Solution {
public double Power(double base, int exponent) throws Exception {
if(exponent == 0){
if(base == 0){
throw new Exception();
}else{
return 1;
}
}

if(exponent == 1){
return base;
}

double flag = exponent > 0 ? 1 : -1;
exponent = exponent > 0 ? exponent : exponent * -1;
double result = powerUnsigned(base, exponent);
return flag == 1 ? result : (1 / result);
}

public double powerUnsigned(double base,int exponent) throws Exception {
if(exponent == 0){
if(base == 0){
throw new Exception();
}else{
return 1;
}
}

if(exponent == 1){
return base;
}

double result = powerUnsigned(base, exponent >> 1);
result *= result;
if((exponent & 1) == 1){
result *= base;
}
return result;
}
}
``````