Asked at
Evaluate Reverse Polish Notation
MediumVerifiedStack~22 min
Given tokens in reverse Polish notation, evaluate the expression and return the integer result. Operators are +, -, *, /; each pops its two operands. Division truncates toward zero.
The input arrives as a single array tokens. Push operands on a stack; on an operator, pop two and push the result.
Examples
in["2", "1", "+", "3", "*"]
out9
(2 + 1) * 3 = 9.
in["4", "13", "5", "/", "+"]
out6
4 + (13 / 5) = 4 + 2 = 6.
Constraints
- 1 ≤ tokens.length ≤ 10⁴
- Each token is an operator (+ - * /) or an integer.
- Division truncates toward zero.
Get help
🔑
Sign in to solve
Sign in to write, run, and submit your solution — and to pick up where your iOS flow left off.