Evaluate Reverse Polish Notation
In Reverse Polish Notation (postfix notation) every operator comes after its two operands, so no parentheses are needed. 3 4 + means 3 + 4, and 3 4 + 2 * means (3 + 4) * 2.
You are given such an expression as an array of strings tokens. Each token is either an integer, possibly negative like "-11", or one of the operators "+", "-", "*" and "/". Evaluate the expression and return its value as a number.
- The expression is always valid: every operator has two operands available, and exactly one value remains at the end.
- Division between integers truncates toward zero:
7 / 2is3and-7 / 2is-3. - There is never a division by zero, and every intermediate result fits in a 32-bit signed integer.
Constraints
1 <= tokens.length <= 10^4- Each token is an operator or an integer in the range
[-200, 200].
Examples
Example 1
Input: tokens = ["2","1","+","3","*"] Output: 9
(2 + 1) * 3 = 9.
Example 2
Input: tokens = ["4","13","5","/","+"] Output: 6
13 / 5 = 2.6, which truncates to 2. Then 4 + 2 = 6.
Example 3
Input: tokens = ["7","-2","/"] Output: -3
7 / -2 = -3.5. Truncating toward zero gives -3, not -4.
JavaScript you'll use: Stacks, queues and deques Numbers, Math and integer tricks
/**
* @param {string[]} tokens
* @return {number}
*/
function evalRPN(tokens) {
}
Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.