current position:Home>leetcode 682. Implementation of JavaScript in baseball game

leetcode 682. Implementation of JavaScript in baseball game

2022-04-29 04:27:54@huihui_ new

/** * @param {string[]} ops * @return {number} */
var calPoints = function(ops) {
    
    let sum = 0;
    const stack = []
    for(let i = 0; i < ops.length; i++) {
    
        if(ops[i] == 'C') {
    
            stack.pop()
        } else if (ops[i] == 'D') {
    
            stack.push(stack[stack.length - 1] * 2)
        } else if(ops[i] == '+') {
    
            stack.push(stack[stack.length - 1] + stack[stack.length - 2])
        } else {
    
            stack.push(parseInt(ops[i]))
        }
    }
    stack.forEach(item => {
    
        sum += item
    })
    return sum
};

copyright notice
author[@huihui_ new],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/117/202204270551339467.html

Random recommended