Intermediate
Open
Pro
Evaluate Expression
Given a string tokens representing an arithmetic expression in
Reverse Polish Notation (RPN), evaluate it and return the
result as an integer.
Valid operators are +, -, *, and /. Each operand may be an
integer or another expression. Division between two integers
should truncate toward zero. You may assume the input is always a
valid RPN expression and that evaluation never causes division by
zero.
Example 1
Input: tokens = ["2", "1", "+", "3", "*"]
Output: 9
Explanation: ((2 + 1) * 3) = 9
Example 2
Input: tokens = ["4", "13", "5", "/", "+"]
Output: 6
Explanation: (4 + (13 / 5)) = 6
Example 3
Input: tokens = ["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"]
Output: 22
Constraints
1 <= tokens.length <= 10^4- Each token is either an operator (
+,-,*,/) or an integer in the range[-200, 200].
Share this question