#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 给定一个后缀表达式,返回它的结果
# @param str string字符串 
# @return long长整型
#
class Solution:
    def legalExp(self , str ):
        # write code here
        stack = []
        num = 0
        for c in str:
            if c != "#" and c != "+" and c != "-" and c != "*":
                num = num*10 + int(c)
            elif c == "#":
                stack.append(num)
                num = 0
            elif c in ["+", "-", "*"]:
                n2, n1 = stack.pop(), stack.pop()
                if c == "+":
                    stack.append(n1 + n2)
                elif c == "-":
                    stack.append(n1 - n2)
                else:
                    stack.append(n1 * n2)

        return stack[-1] if stack else 0