def evalRPN(self, tokens): """ :type tokens: List[str] :rtype: int """ if tokens==None or len(tokens)==0: return 0 num_stack = [] for c in tokens: if c == '+': b = num_stack.pop() a = num_stack.pop() num_stack.append(int(a)+int(b)) elif c == '-': b = num_stack.pop() a = num_stack.pop() num_stack.append(int(a)-int(b)) elif c == '*': b = num_stack.pop() a = num_stack.pop() num_stack.append(int(a)*int(b)) elif c == '/': b = num_stack.pop() a = num_stack.pop() if a/b < 0 and a%b!=0: num_stack.append(a/b+1) else: num_stack.append(a/b) else: num_stack.append(int(c)) return num_stack[0]
评论系统未开启,无法评论!