Evaluate Reverse Polish Notation
Evaluate integer tokens in postfix notation using +, -, * and /. Division truncates toward zero. Return the final integer.
Example 1
tokens = ["5","2","-","4","*"]12Explanation
Compute 5-2=3, then multiply by 4 to obtain 12.
Example 2
tokens = ["-7","3","/"]-2Explanation
-7/3 truncates toward zero to -2 rather than flooring to -3.
Example 3
tokens = ["2","3","4","*","+"]14Explanation
Complete 3*4 before adding 2, producing 14.
Constraints
- 1 <= number of tokens <= 10000
- Expression is valid
- Divisors are nonzero
- Intermediate values fit signed 32-bit integers
Function contract
evalRPNArguments: jsonSolutionRuns on your device
Loading draft…
Run: 3 examples; Submit: 34 cases, on your device.
["5","2","-","4","*"]