diff --git a/solution/0050.Pow(x, n)/Solution.py b/solution/0050.Pow(x, n)/Solution.py new file mode 100644 index 0000000000000..f59be2902c045 --- /dev/null +++ b/solution/0050.Pow(x, n)/Solution.py @@ -0,0 +1,21 @@ +class Solution: + def myPow(self, x: float, n: int) -> float: + + """ + :type x: float + :type n: int + :rtype: float + """ + + answer = 1 + if x == 1 or n == 0: + return 1 + if x == -1: + return 1 if n%2 == 0 else -1 + + for i in range(abs(n)): + answer *= x + if (abs(answer) < 10 ** -5 and n > 0) or (abs(answer) > 10 ** 5 and n < 0): + return 0 + + return answer if n > 0 else 1/answer diff --git a/solution/README.md b/solution/README.md index 142c87519c278..f2f07a877d2a2 100644 --- a/solution/README.md +++ b/solution/README.md @@ -249,6 +249,7 @@ ├── 0050.Pow(x, n) │   ├── Solution.java │   └── Solution.js +│   └── Solution.py ├── 0051.N-Queens │   └── Solution.java ├── 0052.N-Queens II