Swift-数值的整数次幂

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

常规解法

    func power(base:Double,exponent:Int) -> Double {
        if base == 0 {
            return 0
        }
        
        if exponent == 0 {
            return 1
        }
        
        let num:Int = exponent > 0 ? exponent :abs(exponent)
        let result:Double = self.multiWithExponent(base: base, exponet: num)
        //let result:Double = self.multiWithExponentRecursion(base: base, exponet: num)
        //let result:Double = self.multiWithExponentNormal(base: base, exponet: num)
        if exponent < 0 {
            return 1.0/result
        }
        return result
    }
    
    func multiWithExponent(base:Double,exponet:Int) -> Double {
        var result:Double = 1
        for _ in 0..

递归解法

计算结果可以通过递归减少循环的次数,注意奇偶数判断.

    func multiWithExponentRecursion(base:Double,exponet:Int) -> Double {
        if exponet == 0 {
            return 1
        }
        
        if exponet == 1 {
            return base
        }
        
        var result:Double = self.multiWithExponent(base: base, exponet: exponet>>1)
        result *= result
        if  (exponet & 1) == 1 {
            result *= base
        }
        return result
    }

非递归解法

    // 非递归
    func multiWithExponentNormal(base:Double,exponet:Int) -> Double {
        var result:Double = 1.0
        var tempBase:Double = base
        var tempExponet:Int = exponet
        
        while tempExponet > 0 {
            if (tempExponet & 1) == 1{
                result *= tempBase
            }
            tempBase *= tempBase
            tempExponet = tempExponet>>1
        }
        return result
    }

你可能感兴趣的:(Swift-数值的整数次幂)