 All Problems
Evaluate Reverse Polish Notation
medium
stack
math
amazon
linkedin

You are given an array of strings tokens that represents an arithmetic expression in Reverse Polish Notation (RPN).

Evaluate the expression and return the integer result. The valid operators are +, -, *, and /. Division truncates toward zero.

Example 1:

Input: 2 1 + 3 *
Output: 9
Explanation: ((2 + 1) * 3) = 9

Example 2:

Input: 4 13 5 / +
Output: 6
Explanation: (4 + (13 / 5)) = 6

Example 3:

Input: 10 6 9 3 + -11 * / * 17 + 5 +
Output: 22

Constraints:

  • 1 ≤ tokens.length ≤ 10⁴
  • tokens[i] is a valid operator or an integer in the range [-200, 200].

Input format: A single line of space-separated tokens.

Output format: A single integer.

Run to check your code against the sample cases, or submit to run every case