Offer surface prove safety questions 16 (java version): integer power value

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/littlehaes/article/details/91411186

welcome to my blog

Offer surface prove safety questions 16 (java version): integer power value

Title Description

Given a double base type and floating-point type int integer exponent. Seeking exponent of the power base.

Thinking

  • Press the base and exponent value points of the discussion, see specific comments
  • When the exponent is negative, we should pay attention to taking the reciprocal of the base, while the exponent becomes positive number representing the base multiplied by itself exponent times

the complexity

public class Solution {
    public double Power(double base, int exponent) {
        /*
        底数分成两种:(1)非零, (2)0
        指数分成三种:(1)正数, (2)负数, (3)0
        具体地,
            1.底数非零时,分为两种情况
                1.1指数大于等于0:直接求次方
                1.2指数小于0:base要取倒数; 还要注意把exponent变为正数,表示base自乘exponent次
            2.底数是零时,指数需要分情况讨论
                2.1指数是正数:返回0
                2.2指数是0:返回0(这个自己定义)
                2.3指数是负数:不允许,因为0无法作为分母
        */
        double result=1;
        //1. 
        if(base != 0 ){
            //1.2
            if(exponent<0){
                exponent = -1*exponent;
                base = 1/base;
            }
            //1.1
            for(int i=0; i<exponent; i++)
                result *= base;
            return result;
        }
        //2.1, 2.2
        if(exponent>= 0)
            return 0;
        //2.3
        return 0;
  }
}

More efficient code

Thinking

  • Multiplied-by the way is no longer used, the index is incremented by 1
  • Make changes exponentially index, such as index were: 1,2,4,8, ...
  • base ^ exponent results can be divided into two cases according to the parity of the exponent
  • I.: exponent is an even number, then the base ^ exponent = base ^ (exponent / 2) * base ^ (exponent / 2)
  • Case 2: exponent is an odd number, then the base ^ exponent = base ^ ((exponent-1) / 2) * base ^ ((exponent-1) / 2) * base
  • Code (exponent-1) / 2 is written directly exponent / 2 to
  • Indices are divided by 2, will eventually become ... 8,4,2,1,0 attention to find a good recursive termination condition
  • Finally, pay extra attention, in a recursive function, base type is double, because the index is negative, in base to take the reciprocal, so starting a double type, this is a detail
public class Solution {
    public double Power(double base, int exponent) {
        /*
        底数分成两种:(1)非零, (2)0
        指数分成三种:(1)正数, (2)负数, (3)0
        具体地,
            1.底数非零时,分为两种情况
                1.1指数大于等于0:直接求次方
                1.2指数小于0:base要取倒数; 还要注意把exponent变为正数,作为乘几次的标志
            2.底数是零时,指数需要分情况讨论
                2.1指数是正数:返回0
                2.2指数是0:返回0(这个自己定义)
                2.3指数是负数:不允许,因为0无法作为分母
        */
        double result=1;
        //1. 
        if(base != 0 ){
            //1.2
            if(exponent<0){
                exponent = -1*exponent;
                base = 1/base;
            }
            //1.1
            return PowerCore(base, exponent);
        }
        //2.1, 2.2
        if(exponent>= 0)
            return 0;
        //2.3
        return 0;
  }
    private double PowerCore(double base, int exponent){ // 递归函数中, base是double类型, 为了处理负指数,需要把之前的base取到数
        if(exponent==0)
            return 1;
        double result = 1;
        double yinShu = PowerCore(base, exponent/2);
        if((exponent&1)==1)//如果指数是奇数,结果要多乘一个base
            result *= base;
        result = yinShu*yinShu*result;
        return result;
            
    }
}

Guess you like

Origin blog.csdn.net/littlehaes/article/details/91411186