棒球比赛
难度:
标签:
题目描述
You are keeping the scores for a baseball game with strange rules. At the beginning of the game, you start with an empty record.
You are given a list of strings operations
, where operations[i]
is the ith
operation you must apply to the record and is one of the following:
- An integer
x
.- Record a new score of
x
.
- Record a new score of
'+'
.- Record a new score that is the sum of the previous two scores.
'D'
.- Record a new score that is the double of the previous score.
'C'
.- Invalidate the previous score, removing it from the record.
Return the sum of all the scores on the record after applying all the operations.
The test cases are generated such that the answer and all intermediate calculations fit in a 32-bit integer and that all operations are valid.
Example 1:
Input: ops = ["5","2","C","D","+"] Output: 30 Explanation: "5" - Add 5 to the record, record is now [5]. "2" - Add 2 to the record, record is now [5, 2]. "C" - Invalidate and remove the previous score, record is now [5]. "D" - Add 2 * 5 = 10 to the record, record is now [5, 10]. "+" - Add 5 + 10 = 15 to the record, record is now [5, 10, 15]. The total sum is 5 + 10 + 15 = 30.
Example 2:
Input: ops = ["5","-2","4","C","D","9","+","+"] Output: 27 Explanation: "5" - Add 5 to the record, record is now [5]. "-2" - Add -2 to the record, record is now [5, -2]. "4" - Add 4 to the record, record is now [5, -2, 4]. "C" - Invalidate and remove the previous score, record is now [5, -2]. "D" - Add 2 * -2 = -4 to the record, record is now [5, -2, -4]. "9" - Add 9 to the record, record is now [5, -2, -4, 9]. "+" - Add -4 + 9 = 5 to the record, record is now [5, -2, -4, 9, 5]. "+" - Add 9 + 5 = 14 to the record, record is now [5, -2, -4, 9, 5, 14]. The total sum is 5 + -2 + -4 + 9 + 5 + 14 = 27.
Example 3:
Input: ops = ["1","C"] Output: 0 Explanation: "1" - Add 1 to the record, record is now [1]. "C" - Invalidate and remove the previous score, record is now []. Since the record is empty, the total sum is 0.
Constraints:
1 <= operations.length <= 1000
operations[i]
is"C"
,"D"
,"+"
, or a string representing an integer in the range[-3 * 104, 3 * 104]
.- For operation
"+"
, there will always be at least two previous scores on the record. - For operations
"C"
and"D"
, there will always be at least one previous score on the record.
代码结果
运行时间: 12 ms, 内存: 16.2 MB
/*
* 题目思路:
* 1. 创建一个列表来存储每次得分的记录。
* 2. 遍历 ops 数组,根据不同的操作类型更新记录:
* - 整数:直接加入记录。
* - 'C':移除最后一次得分。
* - 'D':加入最后一次得分的两倍。
* - '+':加入最后两次得分的和。
* 3. 使用 Java Stream API 计算记录的总和。
*/
import java.util.ArrayList;
import java.util.List;
public class BaseballGameStream {
public int calPoints(String[] ops) {
List<Integer> record = new ArrayList<>();
for (String op : ops) {
switch (op) {
case "C":
record.remove(record.size() - 1);
break;
case "D":
record.add(record.get(record.size() - 1) * 2);
break;
case "+":
record.add(record.get(record.size() - 1) + record.get(record.size() - 2));
break;
default:
record.add(Integer.parseInt(op));
break;
}
}
return record.stream().mapToInt(Integer::intValue).sum();
}
}
解释
方法:
该题解使用一个栈(列表)来模拟整个计分过程。遍历给定的操作列表,根据不同的操作符执行相应的计分操作。对于整数,直接将其加入到总分中,并将其压入栈中;对于 'C' 操作,从总分中减去栈顶元素,并将栈顶元素弹出;对于 'D' 操作,将栈顶元素的两倍加入到总分中,并将其压入栈中;对于 '+' 操作,将栈顶两个元素的和加入到总分中,并将其压入栈中。最后返回总分即可。
时间复杂度:
O(n)
空间复杂度:
O(n)
代码细节讲解
🦆
在实现中,当遇到'+'操作时,如果栈中不足两个元素该如何处理?题解中提到题目数据保证记录此操作时前面总是存在两个有效的分数,但在实际实现中是否有必要添加检查以避免潜在的错误?
▷🦆
对于`D`操作,题解直接使用了栈顶元素的两倍进行操作,如果栈为空怎么办?是否应该添加对栈为空的检查?
▷🦆
题解没有提及异常处理,例如输入操作为非法字符串(不是'+','D','C'或有效的整数字符串)该如何处理?
▷