数值的整数次方 牛客网 剑指Offer

数值的整数次方 牛客网 剑指Offer

  • 题目描述
  • 给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方

class Solution:
#run:23ms memory:5728k
def Power(self, base, exponent):
flag = 0
if base == 0:
return False
if exponent == 0:
return 1
if exponent < 0:
flag = 1
ret = 1
absExponent = abs(exponent)
ret = self.pow(base,absExponent)
if flag == 1:
ret = 1/ret
return ret def pow(self,X,N):
if N == 0:
return 1
if N == 1:
return X
if N % 2 == 0:
return self.pow(X*X,N/2)
else:
return self.pow(X*X,N/2) * X #run:21ms memory:5856k
def Power2(self,base,exponent):
flag = 0
if base == 0:
return False
if exponent == 0:
return 1
if exponent < 0:
flag = 1
ret = 1
absExponent = abs(exponent)
for i in range(absExponent):
ret *= base
if flag == 1:
ret = 1/ret
return ret
上一篇:数组中只出现过一次的数字 牛客网 剑指Offer


下一篇:一种获取spring环境上下文方法:SpringContextUtil