Skip to content

Latest commit

 

History

History
31 lines (26 loc) · 1001 Bytes

File metadata and controls

31 lines (26 loc) · 1001 Bytes

Stack

Screen Shot 2023-03-02 at 3 47 21 PM

Screen Shot 2023-03-02 at 3 47 30 PM

/**
 * @param {string[]} operations
 * @return {number}
 */
var calPoints = function(operations) {
    let stack = [];

    for(let i = 0; i < operations.length; i++) {
        if(operations[i] === "C") {
            stack.pop();
        } else if(operations[i] === 'D') {
            stack.push(stack[stack.length - 1] * 2);
        } else if(operations[i] === 'C') {
            stack.pop();
        } else if(operations[i] === '+') {
             stack.push(stack[stack.length - 1] + stack[stack.length - 2])
        } else {
            stack.push(+operations[i]);
        }
    }

    return stack.reduce((acc, cur) => acc + cur, 0);
};