You are given an array of strings tokens that represents an arithmetic expression in a Reverse Polish Notation. Evaluate the expression. Return an integer that represents the value of the expression. Note that: - The valid operators are '+', '-', '*', and '/'. - Each operand may be an integer or another expression. - The division between two integers always truncates toward zero. - There will not be any division by zero. - The input represents a valid arithmetic expression in a reverse polish notation. - The answer and all the intermediate calculations can be represented in a 32-bit integer.
Example 1
Input: tokens = ["2","1","+","3","*"]
Output: 9
Explanation: Read left to right using a stack: 2 and 1 are pushed, "+" gives 3, then 3 is pushed and "*" gives 3 * 3 = 9.
Example 2
Input: tokens = ["4","13","5","/","+"]
Output: 6
Explanation: 13 / 5 = 2 (truncated toward zero), then 4 + 2 = 6.
Example 3
Input: tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
Output: 22
Explanation: The expression is 10 * (6 / ((9 + 3) * -11)) + 17 + 5. The inner part is 12 * -11 = -132, and 6 / -132 truncates to 0. So 10 * 0 = 0, and 0 + 17 + 5 = 22.
Reverse Polish Notation puts operators after their operands, which is exactly what a stack wants: push numbers, and when an operator appears, pop the top two, apply it, and push the result back.
Run checks all cases above. Submit evaluates all test cases.