剑指offer-数值的整数次方

来源:互联网 发布:数据库集群方案 编辑:程序博客网 时间:2024/06/07 05:11
public class Solution {    final double INF = 0.00000000001;    public double Power(double base, int exponent) {        double res = 1.0;        if(Math.abs(base-0.0) < INF) return 0.0;        if(exponent==0) return 1;        boolean isNegative = false;        if(exponent < 0) isNegative = true;        exponent = Math.abs(exponent);        //System.out.println(exponent);        res = fastConcluPower(base, exponent);        if(isNegative) res = 1.0 / res;        return res;    }    private double fastConcluPower(double base, int exponent) {        System.out.println(base + " " + exponent);        if(exponent == 0) return 1.0;        int k = exponent / 2;        double res = fastConcluPower(base, k);        if((exponent&1)==1) return res * res * base;        else return res * res;    }}