leetcode——150. 逆波兰表达式求值

class Solution(object):
    def evalRPN(self, tokens):
        """
        :type tokens: List[str]
        :rtype: int
        """
        stack=[]
        for i in tokens:
            if i not in '+-*/':
                stack.append(i)
            else:
                a=stack.pop()
                b=stack.pop()
                if i=='+':
                    c=int(a)+int(b)
                    stack.append(c)
                if i=='-':
                    c = int(b) - int(a)
                    stack.append(c)
                if i=='*':
                    c = int(a) * int(b)
                    stack.append(c)
                if i=='/':
                    c = int(b) / int(a)
                    if int(a)*int(b)<0 and int(b)!=c*int(a):
                        c=c+1
                    stack.append(c)
        return stack.pop()
执行用时 :60 ms, 在所有 python 提交中击败了92.56%的用户
内存消耗 :13.7 MB, 在所有 python 提交中击败了6.25%的用户
 
——2019.11.2
posted @ 2019-11-02 14:47  欣姐姐  阅读(222)  评论(0编辑  收藏  举报