Question description
682. Baseball Game
You are now a recorder of a special game baseball game. This game consists of several rounds, and the scores from the past few rounds may affect the scores from the next few rounds.
At the beginning of the game, the records were blank. You will get a list of strings that record operations ops, where ops[i] is the i-th operation you need to record, and ops follows the following rules:
- integer x - represents the new score x obtained in this round
- "+" - means that the new score obtained in this round is the sum of the previous two scores. The question data ensures that there are always two valid scores before recording this operation.
- "D" - means that the new score obtained in this round is twice the previous score. The question data ensures that there is always a valid score before recording this operation.
- "C" - Indicates that the previous score is invalid and removes it from the record. The question data ensures that there is always a valid score before recording this operation. Please return the sum of all scores in the record.
Example 1:
enter:ops = ["5","2","C","D","+"] Output:30 explain: "5" - Record addition 5 ,Record is now [5] "2" - Record addition 2 ,Record is now [5, 2] "C" - Invalid and remove the previous score,Record is now [5]. "D" - Record addition 2 * 5 = 10 ,Record is now [5, 10]. "+" - Record addition 5 + 10 = 15 ,Record is now [5, 10, 15]. The sum of all scores 5 + 10 + 15 = 30
Example 2:
enter:ops = ["5","-2","4","C","D","9","+","+"] Output:27 explain: "5" - Record addition 5 ,Record is now [5] "-2" - Record addition -2 ,Record is now [5, -2] "4" - Record addition 4 ,Record is now [5, -2, 4] "C" - Invalid and remove the previous score,Record is now [5, -2] "D" - Record addition 2 * -2 = -4 ,Record is now [5, -2, -4] "9" - Record addition 9 ,Record is now [5, -2, -4, 9] "+" - Record addition -4 + 9 = 5 ,Record is now [5, -2, -4, 9, 5] "+" - Record addition 9 + 5 = 14 ,Record is now [5, -2, -4, 9, 5, 14] The sum of all scores 5 + -2 + -4 + 9 + 5 + 14 = 27
Example 3:
enter:ops = ["1"] Output:1
hint:
- 1 <= <= 1000
- ops[i] is "C", "D", "+", or a string representing an integer. The integer range is [-3 * 10^4, 3 * 10^4]
- For the "+" operation, the question data ensures that there are always two valid scores before recording this operation.
- For "C" and "D" operations, the question data ensures that there is always a valid score before recording this operation.
Idea Analysis
Using the stack method, initialize an empty stack_i, and each time you take out a character from the string, the first character is a number, and push the current character to the bottom of the stack.
If C is taken from the character, it means taking out the element at the top of the current stack. If the character is D, it means multiplying the value of the element at the top of the current stack by 2 times, and it is not necessary to obtain a new value.
Replace the value at the top of the stack. Because each operation is independent, it generates an independent value, so the generated value needs to be placed on the top of the stack. If the character taken out is +, it means that the
The value at the top of the current stack and the value below the top of the stack are added to generate a new value. The new value is also obtained by independent operation, so it needs to be pushed into the top of the stack and finally each element of the value in the stack is placed.
Adding together gives you the final answer
AC Code
class Solution: def calPoints(self, ops: List[str]) -> int: stack_i = [] for i in ops: if i == 'C': stack_i.pop() elif i == 'D': stack_i.append(int(stack_i[-1])*2) elif i == '+': stack_i.append(int(stack_i[-1])+ int(stack_i[-2])) else: stack_i.append(int(i)) return(sum(stack_i))
The above is the detailed content of the Go language LeetCode problem solving 682 baseball game. For more information about Go language problem solving baseball game, please pay attention to my other related articles!