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

solution/0050.Pow(x, n)/Solution.py

Lines changed: 21 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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)