Stack Evaluation
Push numbers and let operators consume the latest two values from the stack.
0 of 3 problems solved
Stack Evaluation
Push numbers and let operators consume the latest two values from the stack.
- •Is each operator supposed to use the latest two numbers?
- •Would a stack naturally model that order?
- •Do I push numbers and pop two values on operators?
Reverse Polish Notation stops caring about parentheses because the order is already built into the token stream. Numbers get pushed onto a stack. Operators grab the latest two numbers, compute the result, and push that result back. So the stack acts like a little calculator memory that always exposes the freshest values first.
An operator always consumes the last two available numbers, so a stack is the natural fit.
- 1Push numbers onto the stack.
- 2When you see an operator, pop the top two numbers.
- 3Apply the operator in the correct order.
- 4Push the result back onto the stack.
- 5At the end, the final answer is the only value left.
For subtraction and division, pop order matters. The first popped value is the right operand, not the left one.
Remove All Adjacent Duplicates in String
Push each character onto a stack. Pop the top immediately if it matches the incoming character. Join the stack at the end.
Basic Calculator
Stack for signs: push current result and sign on '(', pop and combine on ')'. Track the current number and running total as you scan character by character.