Search⌘ K
AI Features

Problem: Evaluate Reverse Polish Notation

Understand how to evaluate arithmetic expressions given in Reverse Polish Notation by using a stack to process operands and operators in a single pass. Learn to implement the algorithm in Python, manage integer operations including truncating division, and grasp the efficiency of stack-based solutions.

Statement

You are given an array of strings tokens representing a valid arithmetic expression in Reverse Polish Notation (RPN). Your task is to evaluate the expression and return an integer representing the value of the expression.

Note: The valid operators are '+', '-', '*', and '/'.
Each operand may be an integer or another expression.
Division between two integers always truncates toward zero.
There will not be any division by zero.
The input is guaranteed to represent a valid arithmetic expression in Reverse Polish Notation.
The answer and all intermediate calculations can be represented in a 3232‑bit integer.

Constraints:

  • 11 \leq tokens.length 104\leq 10^4 ...