[topic address]

You are the recorder of a special baseball game. This match is made up of a number of rounds, and points scored in past rounds may affect points scored in future rounds.

At the start of the game, the record was blank. You get a list of strings to record operations ops, where OPS [I] is the ith operation you need to record. Ops follows the following rules:

  1. The integerx– Indicates a new score for the roundx
  2. "+"– Indicates that the new score of the turn is the sum of the previous two scores. The question data ensures that this operation is always preceded by two valid scores.
  3. "D"– Indicates that the new score scored in this turn is twice the previous score. The question data ensures that this operation is always preceded by a valid score when recording it.
  4. "C"– Indicates that the previous score is invalid and removed from the record. The question data ensures that this operation is always preceded by a valid score when recording it.

Return the sum of all the scores in the record.

Example 1:

Ops = ["5","2","C","D","+"] "5" - record plus 5, record is now [5] "2" - record plus 2, record is now [5, 2] "C" - invalidate the record of the previous score and remove it, record is now [5]. "D" - Record plus 2 * 5 = 10, Record is now [5, 10]. "+" - Record plus 5 + 10 = 15, record is now [5, 10, 15]. The sum of all points 5 + 10 + 15 = 30Copy the code

Example 2:

Input: ops = "5", "2", "4", "C", "D", "9", "+", "+"] output: 27: "5" - record plus 5, record is now [5] "-2" - record plus -2, record is now [5, -2] "4" - record plus 4, record is now [5, -2, 4] "C" - invalidate the record of the previous score and remove it, Record is now a "D" - [5-2] record to add 2 * 2 = 4, record is now [5, 2, 4] "9" - record to add 9, record is now [5, 2, 4, 9] "+" - record - 4 + 9 = 5, The record is now [5, -2, -4, 9, 5] "+" - the record plus 9 + 5 = 14, the record is now [5, -2, -4, 9, 5, 14] the sum of all scores 5 + -2 + -4 + 9 + 5 + 14 = 27Copy the code

Example 3:

Input: ops = ["1"] Output: 1Copy the code

Tip:

  • 1 <= ops.length <= 1000
  • ops[i] 为 "C","D","+", or a string representing an integer. The integer range is[-3 * 104, 3 * 104]
  • for"+"Operation, the problem data ensures that recording this operation is always preceded by two valid scores
  • for"C" 和 "D"Operation, the question data ensures that this operation is always preceded by a valid score when recorded

It’s simple, just initialize an array to keep track of all the scores

Then iterate over the OPS and perform the corresponding operation according to the content of each operation

Finally, the sum of all the scores in the score array can be returned

The code is as follows:

var calPoints = function(ops) { const scores = [] for(let i = 0; i<ops.length; i++){ switch(ops[i]){ case '+': // "+" - const len = scores.length scores. Push (scores[len-2]+scores[len-1]) break; Case 'D': // "D" - scores. Push (scores[scores. Length-1]*2) break; Case 'C': // "C" - removes scores.pop() from the record; break; Push (ops[I]*1) break; push(ops[I]*1) break; } // Let res = 0; for(let i = 0; i<scores.length; i++){ res += scores[i] } return res; };Copy the code

At this point we are done with leetcode-682- baseball game

If you have any questions or suggestions, please leave a comment!