2 parents a70af0a + b71c10d commit 54479d1Copy full SHA for 54479d1
1 file changed
leetcode3/정진영/682. Baseball Game
@@ -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