Skip to content

Commit 54479d1

Browse files
Merge pull request #2038 from CodingTestStudy2/정진영
[정진영] Day16
2 parents a70af0a + b71c10d commit 54479d1

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {string[]} operations
3+
* @return {number}
4+
*/
5+
var calPoints = function(operations) {
6+
// stack 사용
7+
// C: pop, D: 2배 push, +: 이전 두 점수 더하고 push
8+
const stack = [];
9+
for (let i = 0; i < operations.length; i++){
10+
if (operations[i] == 'C'){
11+
stack.pop();
12+
} else if (operations[i] == 'D'){
13+
stack.push(stack[stack.length-1]*2);
14+
} else if (operations[i] == '+'){
15+
stack.push(stack[stack.length-1] + stack[stack.length-2]);
16+
} else {
17+
stack.push(Number(operations[i]));
18+
}
19+
}
20+
return stack.reduce((acc, cur) => acc + cur, 0);
21+
};

0 commit comments

Comments
 (0)