Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions leetcode3/정진영/682. Baseball Game
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @param {string[]} operations
* @return {number}
*/
var calPoints = function(operations) {
// stack 사용
// C: pop, D: 2배 push, +: 이전 두 점수 더하고 push
const 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] == '+'){
stack.push(stack[stack.length-1] + stack[stack.length-2]);
} else {
stack.push(Number(operations[i]));
}
}
return stack.reduce((acc, cur) => acc + cur, 0);
};