Open pwstrick opened 4 years ago
682. 棒球比赛
/** * @param {string[]} ops * @return {number} */ var calPoints = function(ops) { const stack = []; ops.forEach(value => { let len = stack.length, sum; if(value == "+" && len > 1) { sum = stack[len-1] + stack[len-2]; stack.push(sum); }else if(value == "D" && len > 0) { sum = stack[len-1] * 2; stack.push(sum); }else if(value == "C" && len > 0) { stack.pop(); }else { stack.push(parseInt(value)); } }); return stack.reduce((acc, current) => acc += current); };
682. 棒球比赛