# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # 给定一个后缀表达式,返回它的结果 # @param str string字符串 # @return long长整型 # class Solution: def legalExp(self, st): stack = [] s = "" num = [str(i) for i in range(10)] fuhao = list("+-*") for ch in st: if ch in num: s = str(s) + ch elif ch == "#": stack.append(int(s)) s="" elif ch in fuhao: if ch == fuhao[0]: a = stack.pop() b = stack.pop() stack.append(b + a) elif ch == fuhao[1]: a = stack.pop() b = stack.pop() stack.append(b - a) elif ch == fuhao[2]: a = stack.pop() b = stack.pop() stack.append(b * a) return stack[-1]