33. Evaluate Reverse Polish Notation

Evaluate an arithmetic expression written in Reverse Polish Notation (operators come after their operands). Operators are + - * /; division truncates toward zero. The expression is always valid.

Example 1

Input: tokens = ["3","4","+","2","*"]
Output: 14
Explanation: (3 + 4) × 2 = 14

Example 2

Input: tokens = ["7","-2","/"]
Output: -3
Explanation: 7 / −2 = −3.5, truncated to −3
Hint 1

Numbers go on a stack; an operator pops the top two numbers.

Hint 2

Pop order matters for − and /: the first pop is the right operand.

StackMath