-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy path棒球比赛.js
More file actions
29 lines (29 loc) · 768 Bytes
/
棒球比赛.js
File metadata and controls
29 lines (29 loc) · 768 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
* @param {string[]} ops
* @return {number}
682. 棒球比赛
遍历数组 整数入栈,栈顶元素,入栈转number类型, + 前两个相加
*/
var calPoints = function(ops) {
const stack = []
for (let i = 0; i < ops.length; i++) {
switch (ops[i]) {
case '+':
stack.push(stack[stack.length - 1] + stack[stack.length - 2])
break;
case 'D':
stack.push(stack[stack.length - 1] * 2)
break
case 'C':
stack.pop()
break
default:
stack.push(Number(ops[i]))
break
}
}
const sum = stack.reduce((sum, val) => {
return sum + val
})
return sum
};