-
Notifications
You must be signed in to change notification settings - Fork 0
/
letcode_50_Pow(x, n).py
57 lines (51 loc) · 1.13 KB
/
letcode_50_Pow(x, 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
# -*- encoding: utf-8 -*-
"""
@File : letcode_50_Pow(x, n).py
@Time : 2020/10/25 上午9:46
@Author : dididididi
@Email :
@Software: PyCharm
"""
class Solutions(object):
def myPow(self, x, n):
"""
:type x: float
:type n: int
:rtype: float
"""
return pow(x, n)
class Solutionss(object):
def myPow(self, x, n):
"""
:type x: float
:type n: int
:rtype: float
"""
if n == 0:
return 1
elif n < 0:
x = 1 / x
n = -n
ans = 1.0
while n > 0:
if n & 1:
ans *= x
x *= x
n >>= 1
return ans
class Solution(object):
def myPow(self, x, n):
"""
:type x: float
:type n: int
:rtype: float
"""
def quickMul(N):
if N == 0:
return 1
y = quickMul(N // 2)
return y * y if N % 2 == 0 else y * y * x
return quickMul(n) if n >= 0 else 1.0 / quickMul(-n)
if __name__ == '__main__':
sol = Solution()
print(sol.myPow(2.00000, 10))