1 """ 2 Evaluate the value of an arithmetic expression in Reverse Polish Notation. 3 Valid operators are +, -, *, /. Each operand may be an integer or another expression. 4 Note: 5 Division between two integers should truncate toward zero. 6 The given RPN expression is always valid. That means the expression would always evaluate to a result and there won't be any divide by zero operation. 7 Example 1: 8 Input: ["2", "1", "+", "3", "*"] 9 Output: 9 10 Explanation: ((2 + 1) * 3) = 9 11 Example 2: 12 Input: ["4", "13", "5", "/", "+"] 13 Output: 6 14 Explanation: (4 + (13 / 5)) = 6 15 Example 3: 16 Input: ["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"] 17 Output: 22 18 Explanation: 19 ((10 * (6 / ((9 + 3) * -11))) + 17) + 5 20 = ((10 * (6 / (12 * -11))) + 17) + 5 21 = ((10 * (6 / -132)) + 17) + 5 22 = ((10 * 0) + 17) + 5 23 = (0 + 17) + 5 24 = 17 + 5 25 = 22 26 """ 27 """ 30The first is arithmetic operations character into integer To 29This question is very simple ideas can be realized with a stack own AC, but encountered trouble three 28 The second is the minuend and subtraction problems. The first pop-up is subtractive, pop-up is the minuend. Also need to consider this division 31 and the third is a problem python rounded down 132 // == -1 -6 32 "" " 33 is class Solution1: 34 is DEF evalRPN (Self, tokens): 35 IF Not tokens: 36 return 0 37 [ Stack = [] 38 is for I in Range (len (tokens)): 39 stack.append (tokens [I]) 40 IF tokens [I] == ' + ' : 41 is stack.pop () 42 is X = int (stack.pop ()) 43 is Y = int (stack.pop ()) 44 is stack.append (Y + X) 45 IF tokens [I] == ' - ' : 46 is stack.pop () 47 X = int (stack.pop ()) 48 Y = int (stack.pop ()) 49 stack.append (YX) # subtractor Note that the first out stack subtraction, division with 50 IF tokens [I] == ' * ' : 51 is stack.pop () 52 is X = int (stack.pop ()) 53 = Y int (stack.pop ()) 54 is stack.append (Y * X) 55 IF tokens [I] == ' / ' : 56 is stack.pop () 57 is X = int (stack.pop ()) 58 Y = int (stack.pop ()) 59 IF X> 0 and Y <0 or X <0 and Y> 0: 60 stack.append (- (ABS (Y) // ABS (X))) 61 is the else : # 6 // - 132 == -1, python rounded down, so the addition of this statement 62 stack.append(y//x) 63 return stack.pop() 64 65 if __name__ == '__main__': 66 ans = Solution1() 67 x = ["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"] 68 s = ans.evalRPN(x) 69 print(s) 70 71 print(6//-132)