Simple JS solution


  • 1
    D
    var calPoints = function(ops) {
        const arr = []
        const actions = {
            C: () => arr.pop(),
            D: () => arr.push(arr[arr.length - 1] * 2),
            '+': () => arr.push(arr[arr.length - 1] + arr[arr.length - 2])
        }
        for (let a of ops) {
            a in actions ? actions[a](): arr.push(+a)
        }
        return arr.reduce((sum, num) => sum + num)
    };
    

Log in to reply
 

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