Skip to content

Commit 4da82cd

Browse files
Add files via upload
1 parent b0f5184 commit 4da82cd

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Pow(x, n)/Pow(x, n).py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# 40ms 94.51%
2+
class Solution:
3+
def myPow(self, x, n):
4+
"""
5+
:type x: float
6+
:type n: int
7+
:rtype: float
8+
"""
9+
if n is 0:
10+
return 1.0
11+
res = 1
12+
abs_n = abs(n)
13+
exponential_item = 1
14+
15+
while exponential_item <= abs_n:
16+
temp = x
17+
while exponential_item * 2 < abs_n:
18+
exponential_item *= 2
19+
temp *= temp
20+
abs_n -= exponential_item
21+
exponential_item = 1
22+
res *= temp
23+
24+
if n > 0:
25+
return res
26+
if n < 0:
27+
return 1 / res

0 commit comments

Comments
 (0)