Evaluate Reverse Polish Notation
Evaluate an arithmetic expression given in Reverse Polish (postfix) notation. tokens is a list where each entry is an integer or one of the operators '+', '-', '*', '/'. Each operator applies to the two most recent operands. Division truncates toward zero. Return the integer result.
Open official problem prompt ↗Compute the numeric value of an expression already written in postfix form.
An old RPN calculator: you key in numbers that stack up, and pressing an operator instantly combines the last two entries into one.
- Input
- tokens = ["2", "1", "+", "3", "*"]
- Output
- 9
- Why
- It encodes (2 + 1) * 3: '+' combines 2 and 1 into 3, then '*' multiplies 3 by 3 to get 9.
1 <= tokens.length <= 10^4Each token is '+', '-', '*', '/' or an integer in [-200, 200]The expression is always a valid RPN expressionDivision between two integers truncates toward zero