-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16_exponent.py
44 lines (36 loc) · 1003 Bytes
/
16_exponent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# -*- coding:utf-8 -*-
class Solution:
def Power(self, base, exponent):
"""计算base的exponent次方
Arguments:
base {int} -- 整数
exponent {int} -- 指数,可正可负
Returns:
int -- 结果
"""
if base == 0:
return 0
if base == 1:
return 1
abs_exponent = self.abs_exp(base, abs(exponent))
if exponent > 0:
return abs_exponent
else:
return 1 / abs_exponent
def abs_exp(self, base, exponent):
if exponent == 0:
return 1
if exponent == 1:
return base
res = self.abs_exp(base, exponent >> 1)
res *= res
if exponent & 1 == 1:
res *= base
return res
if __name__ == "__main__":
base = [2, 3, 0, -2, -3]
exp = [-2, -3, 0, 2, 3]
ex = Solution()
for i in base:
for j in exp:
print(ex.Power(i, j))