Skip to content

Commit 45d5cf7

Browse files
committed
feat: add the solution of Baseball Game(682) with Java.
1 parent 4452e1d commit 45d5cf7

File tree

3 files changed

+103
-25
lines changed

3 files changed

+103
-25
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
| [671][671-question] | [Second Minimum Node In a Binary Tree][671-tips] | [][671-java] | [][671-js] | |
7373
| [674][674-question] | [Longest Continuous Increasing Subsequence][674-tips] | [][674-java] | [][674-js] | |
7474
| [680][680-question] | [Valid Palindrome II][680-tips] | [][680-java] | [][680-js] | |
75-
| [682][682-question] | [Baseball Game][682-tips] | | [][682-js] | |
75+
| [682][682-question] | [Baseball Game][682-tips] | [][682-java] | [][682-js] | |
7676
| [686][686-question] | [Repeated String Match][686-tips] | | [][686-js] | |
7777
| [687][687-question] | [Longest Univalue Path][687-tips] | | [][687-js] | |
7878
| [693][693-question] | [Binary Number with Alternating Bits][693-tips] | | [][693-js] | |
@@ -465,6 +465,7 @@
465465
[671-java]: ./src/_671/Solution.java
466466
[674-java]: ./src/_674/Solution.java
467467
[680-java]: ./src/_680/Solution.java
468+
[682-java]: ./src/_682/Solution.java
468469
[728-java]: ./src/_728/Solution.java
469470
[771-java]: ./src/_771/Solution.java
470471
[804-java]: ./src/_804/Solution.java

src/_682/Solution.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package _682;
2+
3+
class Solution {
4+
public int calPoints(String[] ops) {
5+
int[] scores = new int[1000];
6+
int sum = 0;
7+
int i = 0;
8+
9+
for (String op : ops) {
10+
if (op.charAt(0) == 'C') {
11+
sum -= scores[i--];
12+
continue;
13+
}
14+
15+
i++;
16+
if (op.charAt(0) == '+') {
17+
scores[i] = scores[i-1] + scores[i-2];
18+
} else if (op.charAt(0) == 'D') {
19+
scores[i] = scores[i - 1] * 2;
20+
} else {
21+
scores[i] = Integer.valueOf(op);
22+
}
23+
sum += scores[i];
24+
}
25+
26+
return sum;
27+
}
28+
29+
public static void main(String[] args) {
30+
Solution solution = new Solution();
31+
System.out.println(solution.calPoints(new String[]{"5","-2","4","C","D","9","+","+"}));
32+
}
33+
}

tips/682/README.md

Lines changed: 68 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,90 @@
1-
[xxxx][title]
1+
[Baseball Game][title]
22

33
## Description
4-
// 抄题目
54

5+
You're now a baseball game point recorder.
66

7-
**Example:**
7+
Given a list of strings, each string can be one of the 4 following types:
88

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.
1513

16-
**Tags:** // tags
14+
Each round's operation is permanent and could have an impact on the round before and the round after.
1715

16+
You need to return the sum of the points you could get in all the rounds.
1817

19-
## 思路 1
20-
// 贴一些关键代码,说一些解题思路
21-
// (同一种语言可以写多种思路,与某种语言思路相同的另一种语言的思路无须赘述,但可以把代码贴在后面)
22-
```java
18+
**Example 1:**
2319

2420
```
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.
2729
```
2830

29-
## 思路 2
30-
// 贴一些关键代码,说一些解题思路
31-
```java
31+
**Example 2:**
3232

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.
3345
```
3446

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`代表最后一回合的分数无效。按题意计算分数即可。
3855

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+
}
3983
```
4084

4185
## 结语
42-
86+
4387
如果你同我们一样热爱数据结构、算法、LeetCode,可以关注我们 GitHub 上的 LeetCode 题解:[LeetCode-Solution][ls]
4488

45-
[title]: https://leetcode.com/problems/xxxx
89+
[title]: https://leetcode.com/problems/baseball-game/description/
4690
[ls]: https://github.com/SDE603/LeetCode-Solution

0 commit comments

Comments
 (0)