Skip to content

Commit 8b1d38e

Browse files
committed
add python solution for Problem 50
1 parent 9201868 commit 8b1d38e

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
def myPow(self, x: float, n: int) -> float:
3+
4+
"""
5+
:type x: float
6+
:type n: int
7+
:rtype: float
8+
"""
9+
10+
answer = 1
11+
if x == 1 or n == 0:
12+
return 1
13+
if x == -1:
14+
return 1 if n%2 == 0 else -1
15+
16+
for i in range(abs(n)):
17+
answer *= x
18+
if (abs(answer) < 10 ** -5 and n > 0) or (abs(answer) > 10 ** 5 and n < 0):
19+
return 0
20+
21+
return answer if n > 0 else 1/answer

β€Žsolution/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@
249249
β”œβ”€β”€ 0050.Pow(x, n)
250250
β”‚Β Β  β”œβ”€β”€ Solution.java
251251
β”‚Β Β  └── Solution.js
252+
β”‚Β Β  └── Solution.py
252253
β”œβ”€β”€ 0051.N-Queens
253254
β”‚Β Β  └── Solution.java
254255
β”œβ”€β”€ 0052.N-Queens II

0 commit comments

Comments
Β (0)