forked from qiyuangong/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path050_Pow(x, n).py
More file actions
31 lines (30 loc) · 811 Bytes
/
050_Pow(x, n).py
File metadata and controls
31 lines (30 loc) · 811 Bytes
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
class Solution:
# def myPow(self, x, n):
# """
# :type x: float
# :type n: int
# :rtype: float
# """
# if n == 0:
# return 1
# temp = pow(x, n / 2)
# if n % 2 == 0:
# return temp * temp
# else:
# return temp * temp * x
def myPow(self, x, n):
# https://leetcode.com/discuss/93413/iterative-log-n-solution-with-clear-explanation
# 9 = 2^3 + 2^0 = 1001
# x^9 = x^(2^3)*x(2^0)
# multiple x^i when i place is 1
if n == 0:
return 1
res ,curr = 1, abs(n)
while curr > 0:
if curr & 1 == 1:
res *= x
curr >>= 1
x *= x
if n < 0:
return 1 / res
return res