-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path050_mypow.py
More file actions
42 lines (25 loc) · 775 Bytes
/
Copy path050_mypow.py
File metadata and controls
42 lines (25 loc) · 775 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
32
33
34
35
36
37
38
39
40
41
42
def myPow(x, n):
return x ** n
#use log func
def myPow_demo(x, n):
if n == 0:
return 1
elif n % 2 == 0 and n > 0:
return myPow_demo(x * x, n / 2)
elif n > 0:
return myPow_demo(x, n - 1) * x
else:
return 1 / myPow_demo(x, -n)
print(myPow_demo(2, 10)) #1024
print(myPow_demo(x = 2.10000, n = 3)) #9.26100
print(myPow_demo(x = 2.00000, n = -2))
# class Solution:
# def myPow(self, x: float, n: int) -> float:
# if n == 0:
# return 1
# elif n % 2 == 0 and n > 0:
# return self.myPow(x * x, n / 2)
# elif n > 0:
# return self.myPow(x, n - 1) * x
# else:
# return 1 / self.myPow(x, -n)