|
1 |
| -[xxxx][title] |
| 1 | +[Baseball Game][title] |
2 | 2 |
|
3 | 3 | ## Description
|
4 |
| -// 抄题目 |
5 | 4 |
|
| 5 | +You're now a baseball game point recorder. |
6 | 6 |
|
7 |
| -**Example:** |
| 7 | +Given a list of strings, each string can be one of the 4 following types: |
8 | 8 |
|
9 |
| -``` |
10 |
| -// 抄Example |
11 |
| -``` |
12 |
| - |
13 |
| -**Note:** |
14 |
| -// Note |
| 9 | +1. `Integer` (one round's score): Directly represents the number of points you get in this round. |
| 10 | +2. `"+"` (one round's score): Represents that the points you get in this round are the sum of the last two `valid` round's points. |
| 11 | +3. `"D"` (one round's score): Represents that the points you get in this round are the doubled data of the last `valid` round's points. |
| 12 | +4. `"C"` (an operation, which isn't a round's score): Represents the last `valid` round's points you get were invalid and should be removed. |
15 | 13 |
|
16 |
| -**Tags:** // tags |
| 14 | +Each round's operation is permanent and could have an impact on the round before and the round after. |
17 | 15 |
|
| 16 | +You need to return the sum of the points you could get in all the rounds. |
18 | 17 |
|
19 |
| -## 思路 1 |
20 |
| -// 贴一些关键代码,说一些解题思路 |
21 |
| -// (同一种语言可以写多种思路,与某种语言思路相同的另一种语言的思路无须赘述,但可以把代码贴在后面) |
22 |
| -```java |
| 18 | +**Example 1:** |
23 | 19 |
|
24 | 20 | ```
|
25 |
| -```javascript |
26 |
| - |
| 21 | +Input: ["5","2","C","D","+"] |
| 22 | +Output: 30 |
| 23 | +Explanation: |
| 24 | +Round 1: You could get 5 points. The sum is: 5. |
| 25 | +Round 2: You could get 2 points. The sum is: 7. |
| 26 | +Operation 1: The round 2's data was invalid. The sum is: 5. |
| 27 | +Round 3: You could get 10 points (the round 2's data has been removed). The sum is: 15. |
| 28 | +Round 4: You could get 5 + 10 = 15 points. The sum is: 30. |
27 | 29 | ```
|
28 | 30 |
|
29 |
| -## 思路 2 |
30 |
| -// 贴一些关键代码,说一些解题思路 |
31 |
| -```java |
| 31 | +**Example 2:** |
32 | 32 |
|
| 33 | +``` |
| 34 | +Input: ["5","-2","4","C","D","9","+","+"] |
| 35 | +Output: 27 |
| 36 | +Explanation: |
| 37 | +Round 1: You could get 5 points. The sum is: 5. |
| 38 | +Round 2: You could get -2 points. The sum is: 3. |
| 39 | +Round 3: You could get 4 points. The sum is: 7. |
| 40 | +Operation 1: The round 3's data is invalid. The sum is: 3. |
| 41 | +Round 4: You could get -4 points (the round 3's data has been removed). The sum is: -1. |
| 42 | +Round 5: You could get 9 points. The sum is: 8. |
| 43 | +Round 6: You could get -4 + 9 = 5 points. The sum is 13. |
| 44 | +Round 7: You could get 9 + 5 = 14 points. The sum is 27. |
33 | 45 | ```
|
34 | 46 |
|
35 |
| -## 思路 3 |
36 |
| -// 贴一些关键代码,说一些解题思路 |
37 |
| -```kotlin |
| 47 | +**Note:** |
| 48 | + |
| 49 | +- The size of the input list will be between 1 and 1000. |
| 50 | +- Every integer represented in the list will be between -30000 and 30000. |
| 51 | + |
| 52 | +## 思路 |
| 53 | + |
| 54 | +整数代表所得分数;`+`代表所得分数为最后两回合的有效分数的和;`D`代表所得分数为最后一回合的有效分数的两倍;`C`代表最后一回合的分数无效。按题意计算分数即可。 |
38 | 55 |
|
| 56 | +**Java:** |
| 57 | + |
| 58 | +```java |
| 59 | +public int calPoints(String[] ops) { |
| 60 | + int[] scores = new int[1000]; |
| 61 | + int sum = 0; |
| 62 | + int i = 0; |
| 63 | + |
| 64 | + for (String op : ops) { |
| 65 | + if (op.charAt(0) == 'C') { |
| 66 | + sum -= scores[i--]; |
| 67 | + continue; |
| 68 | + } |
| 69 | + |
| 70 | + i++; |
| 71 | + if (op.charAt(0) == '+') { |
| 72 | + scores[i] = scores[i-1] + scores[i-2]; |
| 73 | + } else if (op.charAt(0) == 'D') { |
| 74 | + scores[i] = scores[i - 1] * 2; |
| 75 | + } else { |
| 76 | + scores[i] = Integer.valueOf(op); |
| 77 | + } |
| 78 | + sum += scores[i]; |
| 79 | + } |
| 80 | + |
| 81 | + return sum; |
| 82 | +} |
39 | 83 | ```
|
40 | 84 |
|
41 | 85 | ## 结语
|
42 |
| - |
| 86 | + |
43 | 87 | 如果你同我们一样热爱数据结构、算法、LeetCode,可以关注我们 GitHub 上的 LeetCode 题解:[LeetCode-Solution][ls]
|
44 | 88 |
|
45 |
| -[title]: https://leetcode.com/problems/xxxx |
| 89 | +[title]: https://leetcode.com/problems/baseball-game/description/ |
46 | 90 | [ls]: https://github.com/SDE603/LeetCode-Solution
|
0 commit comments