#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 给定一个后缀表达式,返回它的结果
# @param str string字符串 
# @return long长整型
#
class Solution:
    def legalExp(self , str ):
        # write code here

        stack = []
        i = 0
        n = len(str)
        
        while i < n:
            if str[i] == '#':
                i+=1
                continue
                
            if str[i].isdigit():
                # 处理多位数
                num = 0
                while i < n and str[i].isdigit():
                    num = num * 10 + int(str[i])
                    i+=1
                stack.append(num)
                continue
                
            elif str[i] == '+':
                a = stack.pop()
                b = stack.pop()
                c = a + b
                stack.append(c) 
                i+=1

            elif str[i] == '*':
                a = stack.pop()
                b = stack.pop()
                c = a * b
                stack.append(c)
                i+=1

            elif str[i] == '-':
                a = stack.pop()
                b = stack.pop()
                c = b - a
                stack.append(c)
                i+=1
                
    
        return int(stack[0])