12 lines javascript


  • -1
    H
    // If you put var ops in 1 line, it can b 8 lines.
    
    var evalRPN = function(tokens) {
        var st=[]
        var ops = {
            '+': (a,b)=>a+b,
            '-': (a,b)=>a-b,
            '*': (a,b)=>a*b,
            '/': (a,b)=>a/b|0,
        }
    
        for(var to of tokens )
            if (to in ops)
                st[st.length-2] = ops[to](st[st.length-2], st.pop())
            else
                st.push(parseInt(to))
    
        return st[0]
    };

Log in to reply
 

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