-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
28 lines (27 loc) · 743 Bytes
/
Copy pathmain.js
File metadata and controls
28 lines (27 loc) · 743 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
/**
* @param {string[]} ops
* @return {number}
*/
var calPoints = function(ops) {
const stack = []
stack.top = function () { return this[this.length - 1] }
stack.lastTwoSum = function () { return this[this.length - 1] + this[this.length - 2] }
return ops.reduce((sum, elt) => {
if (elt === 'C') {
sum -= stack.pop()
return sum
} else if (elt === 'D') {
stack.push(stack.top() * 2)
} else if (elt === '+') {
stack.push(stack.lastTwoSum())
} else {
stack.push(Number(elt))
}
sum += stack.top()
return sum
}, 0)
};
if (process.env.LZS) { // local test
console.log(calPoints(['5', '2', 'C', 'D', '+']))
console.log(calPoints(['5', '-2', '4', 'C', 'D', '9', '+', '+']))
}