问题
解答
栈
如果是数字,则压入栈;如果是符号,则计算。
时间复杂度:O(N)
class Solution:
def evalRPN(self, tokens: List[str]) -> int:
resList = []
if len(tokens) == 0:
return 0
else:
for i in tokens:
if i not in ('+-*/'):
resList.append(i)
else:
b = int(resList.pop())
a = int(resList.pop())
if i == "+":
resList.append(int(a+b))
if i == "-":
resList.append(int(a-b))
if i == "*":
resList.append(int(a*b))
if i == "/":
resList.append(int(a/b))
return int(resList[0])
调试结果: