Evaluate Reverse Polish Notation

Medium

Evaluate an arithmetic expression in reverse Polish notation and return the integer result.

Valid operators are +, -, * and /, and division truncates toward zero.

Example 1

evalRPN()
Input
tokens = ["2","1","+","3","*"]
Output
9

Why

((2 + 1) * 3)

Example 2

evalRPN()
Input
tokens = ["4","13","5","/","+"]
Output
6

Why

(4 + (13 / 5)) with truncated division.
Constraints
1 <= tokens.length <= 10^4
Every division is by a non-zero integer.

Topics

StackMathArrays

Implement evalRPN in the editor and return the answer — printing it is never graded.