题目链接
https://leetcode.cn/problems/evaluate-reverse-polish-notation/description/
代码
仅使用栈
class Solution:
def evalRPN(self, tokens: List[str]) -> int:
stack = []
for i in tokens:
if i not in {'+', '-', '*', '/'}:
stack.append(int(i))
else:
op2 = stack.pop()
op1 = stack.pop()
if i == '+':
stack.append(op1 + op2)
elif i == '-':
stack.append(op1 - op2)
elif i == '*':
stack.append(op1 * op2)
elif i == '/':
stack.append(int(op1 / op2))
return stack.pop()