11 lines JS solution without using eval


  • 0
    L
    function evalRPN(tokens) {
        const ops = {
            '+': (a, b) => b + a,
            '-': (a, b) => b - a,
            '*': (a, b) => b * a,
            '/': (a, b) => parseInt(b / a),
        };
        const stack = [];
        tokens.forEach(t => stack.push(ops[t] ? ops[t](stack.pop(), stack.pop()) : parseInt(t)));
        return stack.pop();
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.