[LeetCode][Python3] 150. Evaluate Reverse Polish Notation
2019. 4. 10. 00:52 |
프로그래밍/LeetCode
Problem :
https://leetcode.com/problems/evaluate-reverse-polish-notation/
My Solution :
class Solution:
def evalRPN(self, tokens: List[str]) -> int:
operators = {
'+': lambda x, y: x+y,
'-': lambda x, y: x-y,
'*': lambda x, y: x*y,
'/': lambda x, y: int(x/y)
}
stack = []
for token in tokens:
if token in operators:
y, x = stack.pop(), stack.pop()
stack.append(operators[token](x, y))
else:
stack.append(int(token))
return stack.pop()
Comment :
사칙연산 문제는 stack을 활용하면 쉽다.
'프로그래밍 > LeetCode' 카테고리의 다른 글
[LeetCode][Python3] 84. Largest Rectangle in Histogram (0) | 2019.04.15 |
---|---|
[LeetCode][Python3] 79. Word Search (0) | 2019.04.13 |
[LeetCode][Python3] 313. Super Ugly Number (0) | 2019.04.13 |
[LeetCode][Python3] 218. The Skyline Problem (0) | 2019.04.11 |
[LeetCode][Python3] 33. Search in Rotated Sorted Array (0) | 2019.04.10 |
[LeetCode][Python3] 227. Basic Calculator II (0) | 2019.04.09 |
[LeetCode][Python3] 134. Gas Station (0) | 2019.04.08 |
[LeetCode][Python3] 23. Merge k Sorted Lists (0) | 2019.04.07 |
최근에 달린 댓글 최근에 달린 댓글