“This is the 16th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

682. Baseball games

The title

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:

The integer x – represents the number of points gained for the turn x “+” – represents the number of points gained for the turn is the sum of the previous two scores. The question data ensures that this operation is always preceded by two valid scores. “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. “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 = 30

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 = 27

Example 3:

Input: ops = [“1”] Output: 1

 

Tip:

1 <= ops. Length <= 1000 OPS [I] is “C”, “D”, “+”, or a string representing an integer. The integer range is [-3 * 104, 3 * 104] for the “+” operation, the problem data guarantees that there will always be a valid score before recording this operation for the “C” and “D” operations

Answer key

Ask questions

  • What data structure is used to solve the problem?

Analysis of the

  • As can be seen from the title, there are four operations in baseballX (integer)Stands for score,+Represents the sum of the previous two scores,CThe previous score is invalid, and the final score needs to be calculated.
  • Can be stacks to simulate this operation, encounteredX (integer)Directly intostackIn thepushx
  • encounter+He wentstackIn thepush stack[stack.length - 1] + stack[stack.length - 2]
  • encounterDHe wentstackIn thepush stack[stack.length - 1] * 2]
  • encounterCitpop
  • At last,reduce((a,b)=> a + b)Calculate the total score

Code implementation

/**
 * @param {string[]} ops
 * @return {number}
 */
var calPoints = function(ops) {
    let stack = []
    for(num of ops){
        switch(num){
            case "C":
                stack.pop()
                break
            case "D":
                stack.push(stack[stack.length - 1] * 2)
                break
            case "+":
                stack.push(stack[stack.length - 1] + stack[stack.length - 2])
                break
            default:
                stack.push(Number(num))
                break
        }
    }
    return stack.reduce((a,b)=> a + b)
};
Copy the code