Problems/150. Evaluate Reverse Polish Notation

150. Evaluate Reverse Polish Notation

MediumArrayMathStack

Compute the final integer value of an arithmetic expression written in postfix notation (Reverse Polish Notation). In postfix notation, operators follow their operands. The operators permitted are +, -, *, and /. Division of integers must discard fractional parts, truncating toward zero. You are guaranteed that the input represents a valid expression and that division by zero will not occur.

Example 1
Input: tokens = ["2","1","+","3","*"]
Output: 9
Equivalent to ((2 + 1) * 3) = 9 in infix notation.
Example 2
Input: tokens = ["4","13","5","/","+"]
Output: 6
Equivalent to (4 + (13 / 5)) = 4 + 2 = 6 (truncating 2.6 to 2).
Example 3
Input: tokens = ["10","6","9","3","+","-11","*","/","*"]
Output: 0
Equivalent to (10 * (6 / ((9 + 3) * -11))) = (10 * (6 / (12 * -11))) = (10 * (6 / -132)) = 10 * 0 = 0.
Visualizer

Visualizer will appear here