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
原文地址:https://www.cnblogs.com/taoyuxin/p/11782379.html
时间: 2024-10-07 17:28:36