題目描述
給定一個double類型的浮點數(shù)base和int類型的整數(shù)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