Skip to content

Commit bc56200

Browse files
committed
682. 棒球比赛
1 parent 628fcc8 commit bc56200

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
|637|[二叉树的层平均值](https://leetcode.cn/problems/average-of-levels-in-binary-tree/)|[JavaScript](./algorithms/average-of-levels-in-binary-tree.js)|Easy|
119119
|645|[错误的集合](https://leetcode.cn/problems/set-mismatch/)|[JavaScript](./algorithms/set-mismatch.js)|Easy|
120120
|657|[机器人能否返回原点](https://leetcode.cn/problems/robot-return-to-origin/)|[JavaScript](./algorithms/robot-return-to-origin.js)|Easy|
121+
|682|[棒球比赛](https://leetcode.cn/problems/baseball-game/)|[JavaScript](./algorithms/baseball-game.js)|Easy|
121122
|687|[最长同值路径](https://leetcode.cn/problems/longest-univalue-path/)|[JavaScript](./algorithms/longest-univalue-path.js)|Medium|
122123
|700|[二叉搜索树中的搜索](https://leetcode.cn/problems/search-in-a-binary-search-tree/)|[JavaScript](./algorithms/search-in-a-binary-search-tree.js)|Easy|
123124
|704|[二分查找](https://leetcode.cn/problems/binary-search/)|[JavaScript](./algorithms/binary-search.js)|Easy|

Diff for: algorithms/baseball-game.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {string[]} ops
3+
* @return {number}
4+
*/
5+
var calPoints = function (ops) {
6+
const stack = [];
7+
8+
for (let s of ops) {
9+
if (+s === +s) {
10+
stack.push(s);
11+
} else if (s === "C") {
12+
stack.pop();
13+
} else if (s === "D") {
14+
stack.push(stack.at(-1) * 2);
15+
} else if (s === "+") {
16+
stack.push(Number(stack.at(-1)) + Number(stack.at(-2)));
17+
}
18+
}
19+
20+
return stack.reduce((a, b) => Number(a) + Number(b), 0);
21+
};

0 commit comments

Comments
 (0)