Offer to come, dig friends take it! I am participating in the 2022 Spring Recruit Punch card activity. Click here for details.

preface

Today’s topic is simple, according to the requirements of the topic can be simulated, very simple, there is a feeling of writing business code, and the algorithm is not linked to.

A daily topic

Today’s topic is 682. Baseball game

  • 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 a new score x for the turn

  • “+” – indicates that the new score 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:

Input: ops = ["5"."2"."C"."D"."+"] output:30Explanation:"5"- record and5, the record is now [5]
"2"- record and2, the record is now [5.2]
"C"- Invalidate the previous score and remove it, the record is now [5].
"D"- record and2 * 5 = 10, the record is now [5.10].
"+"- record and5 + 10 = 15, the record is now [5.10.15The sum of all points5 + 10 + 15 = 30
Copy the code

Example 2:

Input: ops = ["5"."- 2"."4"."C"."D"."9"."+"."+"] output:27Explanation:"5"- record and5, the record is now [5]
"- 2"- Record plus -2, the record is now [5, -2]
"4"- record and4, the record is now [5, -2.4]
"C"- Invalidate the previous score and remove it, the record is now [5, -2]
"D"- record and2 * -2 = -4, the record is now [5, -2, -4]
"9"- record and9, the record is now [5, -2, -4.9]
"+"- Record plus -4 + 9 = 5, the record is now [5, -2, -4.9.5]
"+"- record and9 + 5 = 14, the record is now [5, -2, -4.9.5.14] The sum of all points5 + -2 + -4 + 9 + 5 + 14 = 27
Copy the code

Example 3:

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

 

Tip:

  • 1 <= ops.length <= 1000
  • Ops [I] is “C”, “D”, “+”, or a string representing an integer. Integer range is [-3 * 104, 3 * 104]
  • For the “+” operation, the question data ensures that the operation is always preceded by two valid scores
  • For “C” and “D” operations, the question data ensures that the operation is always preceded by a valid score when recorded

Answer key

Simple simulation

Simple simulation problem, because the problem indicates that several special operations will not be invalid, so it even saves some judgment of boundary conditions, directly according to the requirements of the problem, write down four branches, corresponding to four different input different processing

  1. If the input is’ + ‘, add the first two digits as the value of the first two digits and push them into the array. The first two digits can be retrieved at each loop by obtaining the length of the current array of answers.

  2. Typing ‘C’ pops up the last item of the array

  3. For ‘D’, get the previous item by n again and double it as the value of this item and push it into the array

  4. The rest of the cases are just numbers, so you just push them into an array, and notice that they’re all strings, so you have to do a conversion

  5. Finally, all the values of the answer array are added. Here, the reduce method of the array is used. For details, please refer to MDN

Array.prototype.reduce()

/ * * *@param {string[]} ops
 * @return {number}* /
var calPoints = function (ops) {
  let res = [];
  ops.forEach((e, i) = > {
    let n = res.length;
    if (e == "+") {
      res.push(res[n - 1] + res[n - 2]);
    } else if (e == "C") {
      res.pop();
    } else if (e == "D") {
      res.push(res[n - 1] * 2);
    } else {
      res.push(parseInt(e)); }});return res.reduce((a, b) = > a + b);
};
Copy the code