题目描述
给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。
Python - 偷懒法
class Solution:
def Power(self, base, exponent):
# write code here
return base ** exponent
Python
# -*- coding:utf-8 -*-
class Solution:
def Power(self, base, exponent):
# write code here
if exponent == 0:
return 1
elif exponent > 0:
res = 1
for _ in range(exponent):
res *= base
return res
else:
res = 1
for _ in range(abs(exponent)):
res *= base
return 1 / res
网友评论