1. 程式人生 > >劍指Offer12:數值的整數次方

劍指Offer12:數值的整數次方

思路:

直接呼叫pow函式實現。

# -*- coding:utf-8 -*-
class Solution:
    def Power(self, base, exponent):
        # write code here
        return pow(base,exponent)

另一種實現方法: 

# -*- coding:utf-8 -*-
class Solution:
    def Power(self, base, exponent):
        # write code here
        result=1
        if base==0:
            return 0
        if base==1 or exponent==0:
            return 1
        for i in range(abs(exponent)):
            result*=base
        if exponent<0:
            result=1/result
        return result

fighting