We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent b0f5184 commit 4da82cdCopy full SHA for 4da82cd
Pow(x, n)/Pow(x, n).py
@@ -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
22
+ res *= temp
23
24
+ if n > 0:
25
+ return res
26
+ if n < 0:
27
+ return 1 / res
0 commit comments