-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path50.pow-x-n.py
51 lines (40 loc) · 1.07 KB
/
50.pow-x-n.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
45
46
47
48
49
50
51
#
# @lc app=leetcode.cn id=50 lang=python3
#
# [50] Pow(x, n)
#
# 题解参考极客时间算法课
# 分治递归:每次对半分解任务,时间复杂度O(logN)
# 如果n是偶数,pow(x, n) = pow(x, n/2) * pow(x, n/2)
# 如果n是奇数,pow(x, n) = pow(x, n//2) * pow(x, n//2) * x
# @lc code=start
class Solution:
def myPow(self, x: float, n: int) -> float:
if n == 0:
return 1
elif n == 1:
return x
elif n == -1:
return 1/x
result = self.myPow(x, n//2)
result *= result
if n % 2 != 0:
result *= x
return result
# @lc code=end
# 摘录自极客时间算法课,分治迭代
class Solution2:
def myPow(self, x: float, n: int) -> float:
if n < 1:
x = 1/x
n = -n
res = 1
while n:
if n & 1: # n % 2 != 0
res *= x
x *= x
n >>= 1 # n //= 2
return res
if __name__ == "__main__":
s = Solution2()
assert s.myPow(2, 10) == 1024