解题思路
使用栈
150. 逆波兰表达式求值
代码
class Solution:
def evalRPN(self, tokens: List[str]) -> int:
stack = []
for tok in tokens:
if tok in '+-*/':
second, first = stack.pop(), stack.pop()
if tok == '+': stack.append(first+second)
if tok == '-': stack.append(first-second)
if tok == '*': stack.append(first*second)
if tok == '/': stack.append(int(first/second))
else:
stack.append(int(tok))
return stack[0]

网友评论