leetcode brush test record-682. Baseball game

Offer arrives, dig friends to pick up! I am participating in the 2022 Spring Recruitment Check-In Event, click to view the event details .

foreword

Today's topic is simple. You can simulate it according to the requirements of the topic. It is very simple. It feels like writing business code, and it is not linked to the algorithm at all.

Question of the day

Today's topic is 682. Baseball game , the difficulty is easy

  • You are now the scorer for a special game baseball game. The game consists of several rounds, and the scores of the past rounds may affect the scores of the later rounds.

  • When the game started, the record was blank. You will get a string list ops that records operations, where ops[i] is the i-th operation you need to record, and ops follows these rules:

  • Integer x - Indicates the new score x obtained this round

  • "+" - means that the new score obtained this round is the sum of the previous two scores. The item data guarantees that there are always two valid scores preceding this action when it is recorded.

  • "D" - Indicates that the new score for this round is double the previous score. The item data guarantees that a valid score is always present before this action is recorded.

  • "C" - Indicates that the previous score was invalid and removed from the record. The item data guarantees that a valid score is always present before this action is recorded.

  • Please return the sum of all scores in the record.

 

Example 1:

输入:ops = ["5","2","C","D","+"]
输出:30
解释:
"5" - 记录加 5 ,记录现在是 [5]
"2" - 记录加 2 ,记录现在是 [5, 2]
"C" - 使前一次得分的记录无效并将其移除,记录现在是 [5].
"D" - 记录加 2 * 5 = 10 ,记录现在是 [5, 10].
"+" - 记录加 5 + 10 = 15 ,记录现在是 [5, 10, 15].
所有得分的总和 5 + 10 + 15 = 30
复制代码

Example 2:

输入:ops = ["5","-2","4","C","D","9","+","+"]
输出:27
解释:
"5" - 记录加 5 ,记录现在是 [5]
"-2" - 记录加 -2 ,记录现在是 [5, -2]
"4" - 记录加 4 ,记录现在是 [5, -2, 4]
"C" - 使前一次得分的记录无效并将其移除,记录现在是 [5, -2]
"D" - 记录加 2 * -2 = -4 ,记录现在是 [5, -2, -4]
"9" - 记录加 9 ,记录现在是 [5, -2, -4, 9]
"+" - 记录加 -4 + 9 = 5 ,记录现在是 [5, -2, -4, 9, 5]
"+" - 记录加 9 + 5 = 14 ,记录现在是 [5, -2, -4, 9, 5, 14]
所有得分的总和 5 + -2 + -4 + 9 + 5 + 14 = 27
复制代码

Example 3:

输入:ops = ["1"]
输出:1
复制代码

 

hint:

  • 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 item data guarantees that there are always two valid scores preceding this operation when it is recorded
  • For "C" and "D" actions, the item data guarantees that a valid score is always present before this action is recorded

answer

simple simulation

For a simple simulation question, since the question indicates that several special operations will not be invalid, even the judgment of some boundary conditions is omitted, and directly according to the requirements of the question, write 4 branches, corresponding to 4 different inputs different treatment

  1. If the input is a '+' sign, add the first two digits as the value of this digit and push it into the array. For the first two digits, the length of the current answer array can be obtained in each loop, so that the first two digits can be obtained. position.

  2. If the input is 'C', the last item of the array is popped

  3. If the input is 'D', get the previous item through n and double it as the value of this item and push it into the array

  4. The rest of the cases are all numbers, push directly into the array, and note that the numbers given in the title are all strings, so a conversion is required.

  5. The last step is to add all the values ​​of the answer array. The reduce method of the array is used here. 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);
};
复制代码

image.png

Guess you like

Origin juejin.im/post/7079266581926117389