150. 逆波兰表达式求值

逆波兰就是后序

输入:tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]

输出:22

def evalRPN(self, tokens):

        stack = []

        for token in tokens:

            if token == '+':

                stack.append(int(stack.pop() + stack.pop()))

            elif token == '-':

                stack.append(int(-1 * stack.pop() + stack.pop()))

            elif token == '*':

                stack.append(int(stack.pop() * stack.pop()))

            elif token == '/':

                denometor = stack.pop()

                stack.append(int(stack.pop() / denometor))

            else:

                stack.append(int(token))

        return stack.pop()




python取整方式总结

你可能感兴趣的:(150. 逆波兰表达式求值)