프로그래밍/LeetCode
[LeetCode][Python3] 150. Evaluate Reverse Polish Notation
snoopybox
2019. 4. 10. 00:52
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을 활용하면 쉽다.