Skip to content

Commit bc5e712

Browse files
committed
Add solution and test-cases for problem 682
1 parent 7dbe088 commit bc5e712

File tree

3 files changed

+74
-20
lines changed

3 files changed

+74
-20
lines changed

leetcode/601-700/0682.Baseball-Game/README.md

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,55 @@
11
# [682.Baseball Game][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are keeping score for a baseball game with strange rules. The game consists of several rounds, where the scores of past rounds may affect future rounds' scores.
5+
6+
At the beginning of the game, you start with an empty record. You are given a list of strings `ops`, where `ops[i]` is the i<sup>th</sup> operation you must apply to the record and is one of the following:
7+
8+
1. An integer `x` - Record a new score of `x`.
9+
2. `"+"` - Record a new score that is the sum of the previous two scores. It is guaranteed there will always be two previous scores.
10+
3. `"D"` - Record a new score that is double the previous score. It is guaranteed there will always be a previous score.
11+
4. `"C"` - Invalidate the previous score, removing it from the record. It is guaranteed there will always be a previous score.
12+
13+
Return the sum of all the scores on the record.
14+
715

816
**Example 1:**
917

1018
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
19+
Input: ops = ["5","2","C","D","+"]
20+
Output: 30
21+
Explanation:
22+
"5" - Add 5 to the record, record is now [5].
23+
"2" - Add 2 to the record, record is now [5, 2].
24+
"C" - Invalidate and remove the previous score, record is now [5].
25+
"D" - Add 2 * 5 = 10 to the record, record is now [5, 10].
26+
"+" - Add 5 + 10 = 15 to the record, record is now [5, 10, 15].
27+
The total sum is 5 + 10 + 15 = 30.
1328
```
1429

15-
## 题意
16-
> ...
30+
**Example 2:**
1731

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Baseball Game
23-
```go
2432
```
33+
Input: ops = ["5","-2","4","C","D","9","+","+"]
34+
Output: 27
35+
Explanation:
36+
"5" - Add 5 to the record, record is now [5].
37+
"-2" - Add -2 to the record, record is now [5, -2].
38+
"4" - Add 4 to the record, record is now [5, -2, 4].
39+
"C" - Invalidate and remove the previous score, record is now [5, -2].
40+
"D" - Add 2 * -2 = -4 to the record, record is now [5, -2, -4].
41+
"9" - Add 9 to the record, record is now [5, -2, -4, 9].
42+
"+" - Add -4 + 9 = 5 to the record, record is now [5, -2, -4, 9, 5].
43+
"+" - Add 9 + 5 = 14 to the record, record is now [5, -2, -4, 9, 5, 14].
44+
The total sum is 5 + -2 + -4 + 9 + 5 + 14 = 27.
45+
```
46+
47+
**Example 3:**
2548

49+
```
50+
Input: ops = ["1"]
51+
Output: 1
52+
```
2653

2754
## 结语
2855

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,32 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "strconv"
4+
5+
func Solution(ops []string) int {
6+
ans := 0
7+
8+
points := make([]int, len(ops))
9+
idx := 0
10+
for i := 0; i < len(ops); i, idx = i+1, idx+1 {
11+
op := ops[i]
12+
if op == "C" {
13+
idx -= 2
14+
continue
15+
}
16+
if op == "D" {
17+
points[idx] = points[idx-1] * 2
18+
continue
19+
}
20+
21+
if op == "+" {
22+
points[idx] = points[idx-1] + points[idx-2]
23+
continue
24+
}
25+
_t, _ := strconv.Atoi(op)
26+
points[idx] = _t
27+
}
28+
for i := 0; i < idx; i++ {
29+
ans += points[i]
30+
}
31+
return ans
532
}

leetcode/601-700/0682.Baseball-Game/Solution_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []string
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []string{"5", "2", "C", "D", "+"}, 30},
17+
{"TestCase2", []string{"5", "-2", "4", "C", "D", "9", "+", "+"}, 27},
18+
{"TestCase3", []string{"1"}, 1},
1919
}
2020

2121
// 开始测试

0 commit comments

Comments
 (0)