Evaluate Reverse Polish Notation
MediumP2Coding
Evaluate an arithmetic expression in Reverse Polish Notation (postfix). tokens is an array of strings; each token is either an integer (possibly negative) or one of the operators +, -, *, /.
Division between two integers truncates toward zero. The expression is always valid and never divides by zero. Return the value as an integer.
Example 1
- Input
- tokens = ["2","1","+","3","*"]
- Output
- 9
- Why
- (2 + 1) * 3
Example 2
- Input
- tokens = ["4","13","5","/","+"]
- Output
- 6
- Why
- 4 + (13 / 5) = 4 + 2
Example 3
- Input
- tokens = ["7","-2","/"]
- Output
- -3
- Why
- 7 / -2 = -3.5, truncated toward zero.
Constraints
- 1 <= tokens.length <= 10^4
- Each operand fits in a 32-bit signed integer, and so do all intermediate results
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 11. ⌘Enter runs, ⌘⇧Enter submits.