美文网首页
python实现leetcode之150. 逆波兰表达式求值

python实现leetcode之150. 逆波兰表达式求值

作者: 深圳都这么冷 | 来源:发表于2021-10-18 00:06 被阅读0次

解题思路

使用栈

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]
效果图

相关文章

网友评论

      本文标题:python实现leetcode之150. 逆波兰表达式求值

      本文链接:https://www.haomeiwen.com/subject/mtbuoltx.html