8000
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 339f84b commit a13eb70Copy full SHA for a13eb70
50_Pow_x_n.py
@@ -1,15 +1,15 @@
1
-# 2015-09-07 Runtime: 60 ms
2
-class Solution:
3
- # @param {float} x
4
- # @param {integer} n
5
- # @return {float}
+# 2016-03-15 300 tests, 40 ms
+class Solution(object):
6
def myPow(self, x, n):
7
- # thanks to https://leetcode.com/discuss/9459/o-logn-solution-in-java
8
- if n < 0:
9
- x, n = 1.0 / x, -n
10
- res, f = 1, x
11
- while n > 0:
12
- if n % 2: res *= f
13
- f = f * f
+ """
+ :type x: float
+ :type n: int
+ :rtype: float
+ if n < 0: x, n = 1.0 / x, -n
+ result, weight = 1, x
+ while n: # consider binary
+ if n & 1: result *= weight
+ weight **= 2 # square
14
n >>= 1
15
- return res
+ return result
0 commit comments