-
Notifications
You must be signed in to change notification settings - Fork 43
/
powx-n.py
135 lines (118 loc) · 2.65 KB
/
powx-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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
"""
Implement pow(x, n), which calculates x raised to the power n (i.e., xn).
Example 1:
Input: x = 2.00000, n = 10
Output: 1024.00000
Example 2:
Input: x = 2.10000, n = 3
Output: 9.26100
Example 3:
Input: x = 2.00000, n = -2
Output: 0.25000
Explanation: 2-2 = 1/22 = 1/4 = 0.25
Constraints:
-100.0 < x < 100.0
-231 <= n <= 231-1
-104 <= xn <= 104
"""
# V0
# IDEA : RECURSION
# EXAMPLE :
# myPow(2, 10)
# -> myPow(4, 5)
# -> 4 * myPow(4, 4)
# -> 4 * myPow(16, 2)
# -> 4 * myPow(256, 1)
# -> 4 * 256 * myPow(myPow, 0)
# -> 4 * 256 * 1
# => 1024 (final result)
class Solution(object):
def myPow(self, x, n):
if n == 0:
return 1
if n < 0:
x = 1 / x
n = -n
### NOTE THIS CONDITION
if n % 2 == 1:
return x * self.myPow(x, n - 1) ### NOTE : x * self.myPow(x, n - 1)
return self.myPow(x * x, n / 2) ### NOTE : x * x
# V1
# https://blog.csdn.net/fuxuemingzhu/article/details/82960833
class Solution(object):
def myPow(self, x, n):
"""
:type x: float
:type n: int
:rtype: float
"""
if n == 0:
return 1
if n < 0:
x = 1 / x
n = -n
if n % 2:
return x * self.myPow(x, n - 1)
return self.myPow(x * x, n / 2)
# V1'
# https://blog.csdn.net/fuxuemingzhu/article/details/82960833
# IDEA : ITERATION (二分求冪)
# https://segmentfault.com/a/1190000020655190
class Solution(object):
def myPow(self, x, n):
"""
:type x: float
:type n: int
:rtype: float
"""
if n == 0:
return 1
if n < 0:
x = 1 / x
n = -n
ans = 1
res = 1
while n:
if n % 2:
ans *= x
n >>= 1
x *= x
return ans
# V2
# Time: O(logn) = O(1)
# Space: O(1)
class Solution(object):
def myPow(self, x, n):
"""
:type x: float
:type n: int
:rtype: float
"""
result = 1
abs_n = abs(n)
while abs_n:
if abs_n & 1:
result *= x
abs_n >>= 1
x *= x
return 1 / result if n < 0 else result
# V3
# Time: O(logn)
# Space: O(logn)
# Recursive solution.
class Solution2(object):
def myPow(self, x, n):
"""
:type x: float
:type n: int
:rtype: float
"""
if n < 0 and n != -n:
return 1.0 / self.myPow(x, -n)
if n == 0:
return 1
v = self.myPow(x, n / 2)
if n % 2 == 0:
return v * v
else:
return v * v * x