数值的整数次方

实现函数double Power(double base, int exponent),求base的exponent次方。不得使用库函数,同时不需要考虑大数问题。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/shu-zhi-de-zheng-shu-ci-fang-lcof

这个题我以为很简单,万万没想到啊......

上来直接写代码,一个简单的递归,时间复杂度为log(n)

class Solution {
    public double myPow(double x, int n) {
        if(n==0){return 1;}
        boolean flagZheng = n>0;
        n = Math.abs(n);
        double res = getRes(x,n);
        if(flagZheng){
            return res;
        }else{
            return 1/res;
        }
    }

    private double getRes(double x, int n) {
        if(n==1){return x;}

        return n%2==0?getRes(x,n/2)*getRes(x,n/2):getRes(x,n/2)*getRes(x,n/2)*x;
    }
}

结果超时,百思不得其解。

后来,看评论区找到了原因。

return n%2==0?getRes(x,n/2)*getRes(x,n/2):getRes(x,n/2)*getRes(x,n/2)*x;
这句话乍一看很乱,但是逻辑没问题。但实际上
getRes(x,n/2)算了两次!也就是说,时间复杂度实际上为2倍log(n)
于是我赶快又引入一个变量,代码变成了这样(这种操作得来的值最好给个变量标记一下,也会提高代码整洁度- = )
class Solution {
    public double myPow(double x, int n) {
        if(n==0){return 1;}
        boolean flagZheng = n>0;
        n = Math.abs(n);
        double res = getRes(x,n);
        if(flagZheng){
            return res;
        }else{
            return 1/res;
        }
    }

    private double getRes(double x, int n) {
        if(n==1){return x;}
        double cur = getRes(x,n/2);
        return n%2==0?cur*cur:cur*cur*x;
    }
}

但是,这道题还没有结束!在一开始,我们为了求负数次幂,将直接取绝对值,若n为负,再返回1/res。但是!我们都知道int的取值范围为2的-32次方,到2的32次方减1,若n恰好为2的32次 方减一,取绝对值会溢出!

解决方案为对单独处理

class Solution {
    public double myPow(double x, int n) {
        if(n==0){return 1;}
        boolean flagZheng = n>0;
        if(n==Integer.MIN_VALUE){
            return 1/getRes(x,Integer.MAX_VALUE)*x;
        }
        n = Math.abs(n);
        double res = getRes(x,n);
        if(flagZheng){
            return res;
        }else{
            return 1/res;
        }
    }

    private double getRes(double x, int n) {
        if(n==1){return x;}
        double cur = getRes(x,n/2);
        return n%2==0?cur*cur:cur*cur*x;
    }
}

 

 

posted @ 2020-03-11 11:30  HEUzbc  阅读(151)  评论(0)    收藏  举报