682. Baseball Game Easy

@problem@discussion
#Array#Stack#Simulation



1/**
2 * [682] Baseball Game
3 *
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 * 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^th operation you must apply to the record and is one of the following:
6 * <ol>
7 * 	An integer x - Record a new score of x.
8 * 	"+" - Record a new score that is the sum of the previous two scores. It is guaranteed there will always be two previous scores.
9 * 	"D" - Record a new score that is double the previous score. It is guaranteed there will always be a previous score.
10 * 	"C" - Invalidate the previous score, removing it from the record. It is guaranteed there will always be a previous score.
11 * </ol>
12 * Return the sum of all the scores on the record. The test cases are generated so that the answer fits in a 32-bit integer.
13 *  
14 * Example 1:
15 * 
16 * Input: ops = ["5","2","C","D","+"]
17 * Output: 30
18 * Explanation:
19 * "5" - Add 5 to the record, record is now [5].
20 * "2" - Add 2 to the record, record is now [5, 2].
21 * "C" - Invalidate and remove the previous score, record is now [5].
22 * "D" - Add 2 * 5 = 10 to the record, record is now [5, 10].
23 * "+" - Add 5 + 10 = 15 to the record, record is now [5, 10, 15].
24 * The total sum is 5 + 10 + 15 = 30.
25 * 
26 * Example 2:
27 * 
28 * Input: ops = ["5","-2","4","C","D","9","+","+"]
29 * Output: 27
30 * Explanation:
31 * "5" - Add 5 to the record, record is now [5].
32 * "-2" - Add -2 to the record, record is now [5, -2].
33 * "4" - Add 4 to the record, record is now [5, -2, 4].
34 * "C" - Invalidate and remove the previous score, record is now [5, -2].
35 * "D" - Add 2 * -2 = -4 to the record, record is now [5, -2, -4].
36 * "9" - Add 9 to the record, record is now [5, -2, -4, 9].
37 * "+" - Add -4 + 9 = 5 to the record, record is now [5, -2, -4, 9, 5].
38 * "+" - Add 9 + 5 = 14 to the record, record is now [5, -2, -4, 9, 5, 14].
39 * The total sum is 5 + -2 + -4 + 9 + 5 + 14 = 27.
40 * 
41 * Example 3:
42 * 
43 * Input: ops = ["1","C"]
44 * Output: 0
45 * Explanation:
46 * "1" - Add 1 to the record, record is now [1].
47 * "C" - Invalidate and remove the previous score, record is now [].
48 * Since the record is empty, the total sum is 0.
49 * 
50 *  
51 * Constraints:
52 * 
53 * 	1 <= ops.length <= 1000
54 * 	ops[i] is "C", "D", "+", or a string representing an integer in the range [-3 * 10^4, 3 * 10^4].
55 * 	For operation "+", there will always be at least two previous scores on the record.
56 * 	For operations "C" and "D", there will always be at least one previous score on the record.
57 * 
58 */
59pub struct Solution {}
60
61// problem: https://leetcode.com/problems/baseball-game/
62// discuss: https://leetcode.com/problems/baseball-game/discuss/?currentPage=1&orderBy=most_votes&query=
63
64// submission codes start here
65
66impl Solution {
67    pub fn cal_points(ops: Vec<String>) -> i32 {
68        0
69    }
70}
71
72// submission codes end
73
74#[cfg(test)]
75mod tests {
76    use super::*;
77
78    #[test]
79    fn test_682() {
80    }
81}
82


Back
© 2025 bowen.ge All Rights Reserved.