Skip to content

Commit f4a6c33

Browse files
author
王洋
committed
feat: leetcode 682
1 parent adbe5df commit f4a6c33

File tree

9 files changed

+140
-1
lines changed

9 files changed

+140
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<p align="center">
44
<!-- TOPICS COUNT START -->
5-
<img src="https://img.shields.io/badge/-进度:182-green" alt="进度:182">
5+
<img src="https://img.shields.io/badge/-进度:183-green" alt="进度:183">
66
<!-- TOPICS COUNT END -->
77
<a href="./assets/docs/TOPICS.md"><img src="https://img.shields.io/badge/-题库目录-blue" alt="题库目录"></a>
88
<a href="./assets/docs/CATEGORIES.md"><img src="https://img.shields.io/badge/-题库分类-red" alt="题库分类"></a>

assets/data/categories.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,12 @@
294294
"path": "./problemset/move-zeroes/README.md",
295295
"difficulty": "简单"
296296
},
297+
{
298+
"id": "682",
299+
"title": "棒球比赛",
300+
"path": "./problemset/baseball-game/README.md",
301+
"difficulty": "简单"
302+
},
297303
{
298304
"id": "1144",
299305
"title": "递减元素使数组呈锯齿状",
@@ -1311,6 +1317,12 @@
13111317
"path": "./problemset/number-complement/README.md",
13121318
"difficulty": "简单"
13131319
},
1320+
{
1321+
"id": "682",
1322+
"title": "棒球比赛",
1323+
"path": "./problemset/baseball-game/README.md",
1324+
"difficulty": "简单"
1325+
},
13141326
{
13151327
"id": "1015",
13161328
"title": "可被 K 整除的最小整数",

assets/data/topics.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,6 +1289,16 @@
12891289
"url": "https://leetcode.cn/problems/valid-palindrome-ii/",
12901290
"path": "./problemset/valid-palindrome-ii/README.md"
12911291
},
1292+
{
1293+
"id": "682",
1294+
"title": {
1295+
"cn": "棒球比赛",
1296+
"en": "baseball-game"
1297+
},
1298+
"difficulty": "简单",
1299+
"url": "https://leetcode.cn/problems/baseball-game/description/?envType=daily-question&envId=2024-07-29",
1300+
"path": "./problemset/baseball-game/README.md"
1301+
},
12921302
{
12931303
"id": "746",
12941304
"title": {

assets/docs/CATEGORIES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
| 167. [两数之和 II - 输入有序数组](../../problemset/two-sum-ii-input-array-is-sorted/README.md) | 中等 |
6262
| 215. [数组中的第K个最大元素](../../problemset/kth-largest-element-in-an-array/README.md) | 中等 |
6363
| 283. [移动零](../../problemset/move-zeroes/README.md) | 简单 |
64+
| 682. [棒球比赛](../../problemset/baseball-game/README.md) | 简单 |
6465
| 1144. [递减元素使数组呈锯齿状](../../problemset/decrease-elements-to-make-array-zigzag/README.md) | 中等 |
6566
| 1487. [保证文件名唯一](../../problemset/making-file-names-unique/README.md) | 中等 |
6667
| 2341. [数组能形成多少数对](../../problemset/maximum-number-of-pairs-in-array/README.md) | 简单 |
@@ -268,6 +269,7 @@
268269
| 447. [回旋镖的数量](../../problemset/number-of-boomerangs/README.md) | 中等 |
269270
| 470. [用 Rand7() 实现 Rand10()](../../problemset/implement-rand10-using-rand7/README.md) | 中等 |
270271
| 476. [数字的补数](../../problemset/number-complement/README.md) | 简单 |
272+
| 682. [棒球比赛](../../problemset/baseball-game/README.md) | 简单 |
271273
| 1015. [可被 K 整除的最小整数](../../problemset/smallest-integer-divisible-by-k/README.md) | 中等 |
272274
| 1016. [子串能表示从 1 到 N 数字的二进制串](../../problemset/binary-string-with-substrings-representing-1-to-n/README.md) | 中等 |
273275
| 1072. [按列翻转得到最大值等行数](../../problemset/flip-columns-for-maximum-number-of-equal-rows/README.md) | 中等 |

assets/docs/TOPICS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,8 @@
258258

259259
[680. 验证回文串 II](../../problemset/valid-palindrome-ii/README.md)
260260

261+
[682. 棒球比赛](../../problemset/baseball-game/README.md)
262+
261263
[746. 使用最小花费爬楼梯](../../problemset/min-cost-climbing-stairs/README.md)
262264

263265
[877. 石子游戏](../../problemset/stone-game/README.md)

problemset/baseball-game/README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# 棒球比赛
2+
3+
> 难度:简单
4+
>
5+
> https://leetcode.cn/problems/baseball-game/description/?envType=daily-question&envId=2024-07-29
6+
7+
## 题目
8+
9+
你现在是一场采用特殊赛制棒球比赛的记录员。这场比赛由若干回合组成,过去几回合的得分可能会影响以后几回合的得分。
10+
11+
比赛开始时,记录是空白的。你会得到一个记录操作的字符串列表 `ops`,其中 `ops[i]` 是你需要记录的第 `i` 项操作,`ops` 遵循下述规则:
12+
13+
- 整数 `x` - 表示本回合新获得分数 `x`
14+
- `"+"` - 表示本回合新获得的得分是前两次得分的总和。题目数据保证记录此操作时前面总是存在两个有效的分数。
15+
- `"D"` - 表示本回合新获得的得分是前一次得分的两倍。题目数据保证记录此操作时前面总是存在一个有效的分数。
16+
- `"C"` - 表示前一次得分无效,将其从记录中移除。题目数据保证记录此操作时前面总是存在一个有效的分数。
17+
18+
请你返回记录中所有得分的总和。
19+
20+
21+
22+
### 示例 1:
23+
```
24+
输入:ops = ["5","2","C","D","+"]
25+
输出:30
26+
解释:
27+
"5" - 记录加 5 ,记录现在是 [5]
28+
"2" - 记录加 2 ,记录现在是 [5, 2]
29+
"C" - 使前一次得分的记录无效并将其移除,记录现在是 [5].
30+
"D" - 记录加 2 * 5 = 10 ,记录现在是 [5, 10].
31+
"+" - 记录加 5 + 10 = 15 ,记录现在是 [5, 10, 15].
32+
所有得分的总和 5 + 10 + 15 = 30
33+
```
34+
35+
### 示例 2:
36+
```
37+
输入:ops = ["5","-2","4","C","D","9","+","+"]
38+
输出:27
39+
解释:
40+
"5" - 记录加 5 ,记录现在是 [5]
41+
"-2" - 记录加 -2 ,记录现在是 [5, -2]
42+
"4" - 记录加 4 ,记录现在是 [5, -2, 4]
43+
"C" - 使前一次得分的记录无效并将其移除,记录现在是 [5, -2]
44+
"D" - 记录加 2 * -2 = -4 ,记录现在是 [5, -2, -4]
45+
"9" - 记录加 9 ,记录现在是 [5, -2, -4, 9]
46+
"+" - 记录加 -4 + 9 = 5 ,记录现在是 [5, -2, -4, 9, 5]
47+
"+" - 记录加 9 + 5 = 14 ,记录现在是 [5, -2, -4, 9, 5, 14]
48+
所有得分的总和 5 + -2 + -4 + 9 + 5 + 14 = 27
49+
```
50+
51+
### 示例 3:
52+
```
53+
输入:ops = ["1"]
54+
输出:1
55+
```

problemset/baseball-game/index.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from typing import List
2+
3+
class Solution:
4+
def calPoints(self, operations: List[str]) -> int:
5+
stack = []
6+
for op in operations:
7+
if op == 'C':
8+
stack.pop()
9+
elif op == 'D':
10+
stack.append(stack[-1] * 2)
11+
elif op == '+':
12+
stack.append(stack[-1] + stack[-2])
13+
else:
14+
stack.append(int(op))
15+
return sum(stack)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { calPoints } from '.'
3+
4+
describe('棒球比赛', () => {
5+
testCase(calPoints)
6+
})
7+
8+
function testCase(fn: (operations: string[]) => number) {
9+
it.each([
10+
// test cases
11+
[['5', '2', 'C', 'D', '+'], 30],
12+
[['5', '-2', '4', 'C', 'D', '9', '+', '+'], 27],
13+
[['1'], 1],
14+
])('示例%#', (operations, expected) => {
15+
expect(fn(operations)).toBe(expected)
16+
})
17+
}

problemset/baseball-game/index.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export function calPoints(operations: string[]): number {
2+
if (operations.length === 0) {
3+
return 0;
4+
}
5+
const arr: number[] = [];
6+
let res = 0;
7+
while (operations.length > 0) {
8+
const x = operations.shift() as string;
9+
if (x === '+') {
10+
arr.push(arr[arr.length - 1] + arr[arr.length - 2])
11+
res += arr[arr.length - 1];
12+
} else if (x === 'D') {
13+
arr.push(arr[arr.length - 1] * 2)
14+
res += arr[arr.length - 1];
15+
} else if (x === 'C') {
16+
res -= arr[arr.length - 1];
17+
arr.pop();
18+
} else {
19+
arr.push(parseInt(x))
20+
res += arr[arr.length - 1];
21+
}
22+
}
23+
24+
return res;
25+
// return arr.reduce((prev, curr) => prev + curr, 0);
26+
}

0 commit comments

Comments
 (0)