Evaluate Reverse Polish Notation
You get an arithmetic expression in reverse Polish notation, as a list of tokens: integers and the operators + − × ÷, each operator after its two operands. Work out its value. Division keeps only the integer part, rounding towards zero, and the expression is always correct. Our example: 3 10 − 4 × 5 ÷, which is (3 − 10) × 4 ÷ 5 — the answer is −5.
- input
tokens: an arithmetic expression in reverse Polish notation, one token per item: an integer or one of+,-,*,/. An operator applies to the two values before it.- returns
- The value of the expression. Division between integers rounds toward zero:
-7 / 2is-3. - constraints
- 1 ≤ tokens.length ≤ 5000
- tokens[i]: + - * / or an integer in [−200, 200]
- tokens is a valid expression, with no division by zero
- the answer and every result along the way fit in a 32-bit integer
- examples
- Open a test in the tests panel: it shows the input and the expected answer.
/**
* @param {string[]} tokens
* @return {number}
*/
function evalRPN(tokens) {
}
Ctrl ↵
format
reset
saved in this browser
Stuck?
See the solution run step by step on our example, then why it works and its complexity.
Next: more stack problems
The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.