|  | 
| 1 | 1 | # [682.Baseball Game][title] | 
| 2 | 2 | 
 | 
| 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 |  | -
 | 
| 6 | 3 | ## 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 | + | 
| 7 | 15 | 
 | 
| 8 | 16 | **Example 1:** | 
| 9 | 17 | 
 | 
| 10 | 18 | ``` | 
| 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. | 
| 13 | 28 | ``` | 
| 14 | 29 | 
 | 
| 15 |  | -## 题意 | 
| 16 |  | -> ... | 
|  | 30 | +**Example 2:** | 
| 17 | 31 | 
 | 
| 18 |  | -## 题解 | 
| 19 |  | - | 
| 20 |  | -### 思路1 | 
| 21 |  | -> ... | 
| 22 |  | -Baseball Game | 
| 23 |  | -```go | 
| 24 | 32 | ``` | 
|  | 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:** | 
| 25 | 48 | 
 | 
|  | 49 | +``` | 
|  | 50 | +Input: ops = ["1"] | 
|  | 51 | +Output: 1 | 
|  | 52 | +``` | 
| 26 | 53 | 
 | 
| 27 | 54 | ## 结语 | 
| 28 | 55 | 
 | 
|  | 
0 commit comments