Back to course home
0% completed
Vote For New Content
Evaluate Reverse Polish Notation (medium)
Problem Statement
Given an array of strings tokens
that represents an arithmetic expression in Reverse Polish Notation (RPN), evaluate
the expression and return the resulting integer value.
- The valid operators are
'+'
,'-'
,'*'
, and'/'
. - Each operand can be an integer or another expression.
- Division between two integers should truncate towards zero.
- No division by zero will occur.
- The input is a valid RPN expression.
- All results fit within a 32-bit integer.
Examples
Example 1
- Input: tokens =
["2", "11", "5", "/", "+"]
- Output:
4
- Explanation: (11 / 5) = 2, then (2 + 2) = 4.
Example 2
- Input: tokens =
["2", "3", "11", "+", "*"]
- Output:
28
- Explanation: (11 + 3) = 14, then (2 * 14) = 28.
Example 3
- Input: tokens =
["5", "1", "2", "+", "4", "*", "+", "3", "-"]
- Output:
14
- Explanation: (1 + 2) = 3, (3 * 4) = 12, (5 + 12) = 17, (17 - 3) = 14.
Constraints:
- 1 <= tokens.length <= 10<sup>4</sup>
tokens[i]
is either an operator: "+", "-", "*", or "/", or an integer in the range[-200, 200]
.
Try it yourself
Try solving this question here:
Python3
Python3
. . . .
.....
.....
.....
Like the course? Get enrolled and start learning!
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible