Applied AI Engineer (LLM) coding learning path Level 2 This level · 14 of 31

Evaluate Reverse Polish Notation

Medium Stack

Problem

Evaluate an arithmetic expression written in reverse Polish notation. The valid operators are +, -, * and /, and division between two integers truncates toward zero.

Original: LeetCode 150: Evaluate Reverse Polish Notation.

Examples

Example 1

Input`tokens = ["2", "1", "+", "3", "*"]`
Output`9`

((2 + 1) * 3) = 9

Example 2

Input`tokens = ["4", "13", "5", "/", "+"]`
Output`6`

(4 + (13 / 5)) = 6, because division truncates toward zero.

Example 3

Input`tokens = ["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"]`
Output`22`

Constraints

  • 1 <= tokens.length <= 10^4
  • tokens[i] is either an operator: "+", "-", "*", or "/", or an integer in the range [-200, 200].

Hints

Solution

Tried it yourself? The approach, complexity, and code stay hidden until you choose.

Sources (1)
  1. LeetCode 150: Evaluate Reverse Polish Notationleetcode.com
Previous
Next