利用栈的思想:
如果是数字,则压入栈;若为符号,即执行“pop取栈顶 -> pop取新栈顶 -> 计算”操作。
具体代码为
class Solution:
def evalRPN(self, tokens: List[str]) -> int:
symbol = ["+","-","*","/"]
List1 = []
if len(tokens) == 0: return 0
else:
for a in tokens:
if a not in symbol:
List1.append(a)
else:
b = int(List1.pop())
c = int(List1.pop())
if a == "+":
List1.append(str(c+b))
elif a == "-":
List1.append(str(c-b))
elif a == "*":
List1.append(str(c*b))
elif a == "/":
List1.append(str(int(c/b)))
return List1[0]