[Q] [Medium]
Implement pow(x, n), which calculates x raised to the power n (i.e., xn).
- pow(x, n) 을 구현하라고 해서 ** 로 return 했는데 통과
- 뭔가 Medium 문제 인데 이렇게 풀어도 되나 다른 사람들 답도 찾아봤다.
# Answer
class Solution:
def myPow(self, x: float, n: int) -> float:
return x ** n
# 다른 사람 답
class Solution:
def myPow(self, x: float, n: int) -> float:
my_pow_num = pow(x, n)
if my_pow_num > (pow(2, 31) - 1):
my_pow_num = pow(2, 31) -1
elif my_pow_num < (pow(2, 31)) * (-1):
my_pow_num = pow(2, 31) * (-1)
return my_pow_num
'Algorithm > Leetcode' 카테고리의 다른 글
[Top Interview 150 - Math] 69. Sqrt(x) (1) | 2024.10.13 |
---|---|
[Top Interview 150 - Math] 172. Factorial Trailing Zeroes (1) | 2024.10.13 |
[Top Interview 150 - Math] 9. Palindrome Number (0) | 2024.10.13 |
[Top Interview 150 - Hashmap] 128. Longest Consecutive Sequence (0) | 2024.10.13 |
[Top Interview 150 - Hashmap] 219. Contains Duplicate II (1) | 2024.10.12 |