* https://leetcode.com/problems/evaluate-reverse-polish-notation
* Time O(N^2) | Space(1)
* @param {string[]} tokens
* @return {number}
*/
var evalRPN = function (tokens, index = 0) {
while (1 < tokens.length) {
const isOperation = () => tokens[index] in OPERATORS;
while (!isOperation()) index++;
const value = performOperation(tokens, index);
tokens[index] = value;
tokens.splice(index - 2, 2);
index--;
}
return tokens[0];
};
var OPERATORS = {
'+': (a, b) => a + b,
'-': (a, b) => a - b,
'*': (a, b) => a * b,
'/': (a, b) => Math.trunc(a / b),
};
var performOperation = (tokens, index) => {
const [rightNum, leftNum] = [
Number(tokens[index - 1]),
Number(tokens[index - 2]),
];
const operation = OPERATORS[tokens[index]];
return operation(leftNum, rightNum);
};
* https://leetcode.com/problems/evaluate-reverse-polish-notation
* Time O(N) | Space(N)
* @param {string[]} tokens
* @return {number}
*/
var evalRPN = function (tokens, stack = []) {
for (const char of tokens) {
const isOperation = char in OPERATORS;
if (isOperation) {
const value = performOperation(char, stack);
stack.push(value);
continue;
}
stack.push(Number(char));
}
return stack.pop();
};
var OPERATORS = {
'+': (a, b) => a + b,
'-': (a, b) => a - b,
'*': (a, b) => a * b,
'/': (a, b) => Math.trunc(a / b),
};
var performOperation = (char, stack) => {
const [rightNum, leftNum] = [stack.pop(), stack.pop()];
const operation = OPERATORS[char];
return operation(leftNum, rightNum);
};