150. 逆波兰表达式求值 - 力扣(LeetCode)
class Solution:def evalRPN(self, tokens: List[str]) -> int:stack = []for token in tokens:if token not in {'+', '-', '*', '/'}: # 如果是数字stack.append(int(token))else: # 如果是操作符b = stack.pop()a = stack.pop()if token == '+':stack.append(a + b)elif token == '-':stack.append(a - b)elif token == '*':stack.append(a * b)elif token == '/':# Python 的整数除法默认向下取整,# 这里用 int(a / b) 来确保向零截断。stack.append(int(a / b))return stack[0] # 最后栈中只剩下一个结果