-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path50.py
27 lines (27 loc) · 1013 Bytes
/
50.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
__________________________________________________________________________________________________
sample 24 ms submission
class Solution:
def myPow(self, x: float, n: int) -> float:
if n == 0:
# if not n:
return 1
if n < 0:
return 1 / self.myPow(x, -n)
if n % 2:
return x * self.myPow(x, n-1)
return self.myPow(x*x, n//2)
# return self.myPow(x**2,n//2) if n%2 == 0 else x*self.myPow(x,n-1)
__________________________________________________________________________________________________
sample 12776 kb submission
class Solution:
def myPow(self, x: float, n: int) -> float:
if x ==0 or x == 1 or n ==1:
return x
if n == 0:
return 1
if n < 0:
x, n =1/x, abs(n)
if n%2 ==0:
return self.myPow(x*x, n/2)
return self.myPow(x, n-1)*x
__________________________________________________________________________________________________