#150

Evaluate Reverse Polish Notation

Medium Operand stack

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Evaluate an arithmetic expression in Reverse Polish (postfix) notation with + - * / on integers. Division truncates toward zero.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: compute a postfix expression.

Why this works: numbers go on a stack; an operator pops two, applies, and pushes the result.

Example: ["2", "1", "+", "3", "*"]

2, 1 → + → 33, 3 → * → 9

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Swapping operands for - and /, giving b − a instead of a − b.

Solve on LeetCode ↗
Complexity
  • Time: O(n).
  • Space: O(n).
Topic: Stack & Queue
🎬 Episodes 46–52: video coming soon