Medium · Stack
Understand → Visualise → Practice.
In reverse Polish (postfix) notation the operator comes after its operands: 3 4 + means 3 + 4. No brackets are ever needed.
Push numbers. When you meet an operator, pop the top two values, apply it, and push the result.
Push each operand.
b = pop(), a = pop(), push a op b.
That value is the answer.
O(n) time, O(n) space.
Order matters for − and /: the first value popped is the right-hand operand. Division truncates toward zero.
Read the full guides first: Stacks, Queues and Deques.
Guest mode lets you understand, visualise and run the sample tests. Create a free account to submit and save your solutions.