难度:中等
题目描述:
思路总结:这个没啥说的,中缀表达式,唯一值得注意的是Python中的//是向下取整的,也就是说-0.1会向下取整为-1。
题解一:
class Solution:
def evalRPN(self, tokens: List[str]) -> int:
stack = []
operators = ["+", "-", "*", "/"]
for i in range(len(tokens)):
if tokens[i] in operators:
r = stack.pop()
l = stack.pop()
if tokens[i] == "+":
stack.append(l+r)
elif tokens[i] == "-":
stack.append(l-r)
elif tokens[i] == "*":
stack.append(l*r)
elif tokens[i] == "/":
stack.append(int(l/r))
else:
stack.append(int(tokens[i]))
return stack.pop()