题目:
给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。
解法1:分三种情况,exponent大于0,exponent小于0,exponent等于0。
public class Solution {
    public double Power(double base, int exponent) {
        double result=1.0;  if(exponent>0) {  for(int i=0;i<exponent;i++) {  result=result*base;  }  }else if(exponent<0) {  exponent=-exponent;  for(int i=0;i<exponent;i++) {  result=result*base;  }  result=1/result;  }else {  result=1;  }  return result;  }
}
上面那种方法的时间复杂度为O(n),还有一种时间复杂度为O(logn)的方法。
public class Solution {
    public double Power(double base, int exponent) {
        double res=1.0;         if(exponent>0) {             res=Function(base, exponent);         }else if(exponent<0) {             exponent=-exponent;             res=1/Function(base, exponent);         }else {             res=1;         }         return res;     }     public double Function(double base, int exponent) {
        if (exponent==0) {
            return 1;
        }
        if (exponent==1) {
            return base;
        }             double result=Power(base, exponent>>1);             result*=result;             if ((exponent&1)==1) {                 result*=base;             }             return result;         }
}