-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
王洋
committed
Jul 29, 2024
1 parent
adbe5df
commit f4a6c33
Showing
9 changed files
with
140 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# 棒球比赛 | ||
|
||
> 难度:简单 | ||
> | ||
> https://leetcode.cn/problems/baseball-game/description/?envType=daily-question&envId=2024-07-29 | ||
## 题目 | ||
|
||
你现在是一场采用特殊赛制棒球比赛的记录员。这场比赛由若干回合组成,过去几回合的得分可能会影响以后几回合的得分。 | ||
|
||
比赛开始时,记录是空白的。你会得到一个记录操作的字符串列表 `ops`,其中 `ops[i]` 是你需要记录的第 `i` 项操作,`ops` 遵循下述规则: | ||
|
||
- 整数 `x` - 表示本回合新获得分数 `x` | ||
- `"+"` - 表示本回合新获得的得分是前两次得分的总和。题目数据保证记录此操作时前面总是存在两个有效的分数。 | ||
- `"D"` - 表示本回合新获得的得分是前一次得分的两倍。题目数据保证记录此操作时前面总是存在一个有效的分数。 | ||
- `"C"` - 表示前一次得分无效,将其从记录中移除。题目数据保证记录此操作时前面总是存在一个有效的分数。 | ||
|
||
请你返回记录中所有得分的总和。 | ||
|
||
|
||
|
||
### 示例 1: | ||
``` | ||
输入:ops = ["5","2","C","D","+"] | ||
输出:30 | ||
解释: | ||
"5" - 记录加 5 ,记录现在是 [5] | ||
"2" - 记录加 2 ,记录现在是 [5, 2] | ||
"C" - 使前一次得分的记录无效并将其移除,记录现在是 [5]. | ||
"D" - 记录加 2 * 5 = 10 ,记录现在是 [5, 10]. | ||
"+" - 记录加 5 + 10 = 15 ,记录现在是 [5, 10, 15]. | ||
所有得分的总和 5 + 10 + 15 = 30 | ||
``` | ||
|
||
### 示例 2: | ||
``` | ||
输入:ops = ["5","-2","4","C","D","9","+","+"] | ||
输出:27 | ||
解释: | ||
"5" - 记录加 5 ,记录现在是 [5] | ||
"-2" - 记录加 -2 ,记录现在是 [5, -2] | ||
"4" - 记录加 4 ,记录现在是 [5, -2, 4] | ||
"C" - 使前一次得分的记录无效并将其移除,记录现在是 [5, -2] | ||
"D" - 记录加 2 * -2 = -4 ,记录现在是 [5, -2, -4] | ||
"9" - 记录加 9 ,记录现在是 [5, -2, -4, 9] | ||
"+" - 记录加 -4 + 9 = 5 ,记录现在是 [5, -2, -4, 9, 5] | ||
"+" - 记录加 9 + 5 = 14 ,记录现在是 [5, -2, -4, 9, 5, 14] | ||
所有得分的总和 5 + -2 + -4 + 9 + 5 + 14 = 27 | ||
``` | ||
|
||
### 示例 3: | ||
``` | ||
输入:ops = ["1"] | ||
输出:1 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from typing import List | ||
|
||
class Solution: | ||
def calPoints(self, operations: List[str]) -> int: | ||
stack = [] | ||
for op in operations: | ||
if op == 'C': | ||
stack.pop() | ||
elif op == 'D': | ||
stack.append(stack[-1] * 2) | ||
elif op == '+': | ||
stack.append(stack[-1] + stack[-2]) | ||
else: | ||
stack.append(int(op)) | ||
return sum(stack) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { calPoints } from '.' | ||
|
||
describe('棒球比赛', () => { | ||
testCase(calPoints) | ||
}) | ||
|
||
function testCase(fn: (operations: string[]) => number) { | ||
it.each([ | ||
// test cases | ||
[['5', '2', 'C', 'D', '+'], 30], | ||
[['5', '-2', '4', 'C', 'D', '9', '+', '+'], 27], | ||
[['1'], 1], | ||
])('示例%#', (operations, expected) => { | ||
expect(fn(operations)).toBe(expected) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
export function calPoints(operations: string[]): number { | ||
if (operations.length === 0) { | ||
return 0; | ||
} | ||
const arr: number[] = []; | ||
let res = 0; | ||
while (operations.length > 0) { | ||
const x = operations.shift() as string; | ||
if (x === '+') { | ||
arr.push(arr[arr.length - 1] + arr[arr.length - 2]) | ||
res += arr[arr.length - 1]; | ||
} else if (x === 'D') { | ||
arr.push(arr[arr.length - 1] * 2) | ||
res += arr[arr.length - 1]; | ||
} else if (x === 'C') { | ||
res -= arr[arr.length - 1]; | ||
arr.pop(); | ||
} else { | ||
arr.push(parseInt(x)) | ||
res += arr[arr.length - 1]; | ||
} | ||
} | ||
|
||
return res; | ||
// return arr.reduce((prev, curr) => prev + curr, 0); | ||
} |