[路飞]_每天刷leetcode_19(棒球比赛 baseball game)

441 阅读2分钟

棒球比赛

LeetCode传送门682. Baseball Game

题目

你现在是一场采用特殊赛制棒球比赛的记录员。这场比赛由若干回合组成,过去几回合的得分可能会影响以后几回合的得分。

比赛开始时,记录是空白的。你会得到一个记录操作的字符串列表 ops,其中 ops[i] 是你需要记录的第 i 项操作,ops 遵循下述规则:

整数 x - 表示本回合新获得分数 x "+" - 表示本回合新获得的得分是前两次得分的总和。题目数据保证记录此操作时前面总是存在两个有效的分数。 "D" - 表示本回合新获得的得分是前一次得分的两倍。题目数据保证记录此操作时前面总是存在一个有效的分数。 "C" - 表示前一次得分无效,将其从记录中移除。题目数据保证记录此操作时前面总是存在一个有效的分数。 请你返回记录中所有得分的总和。

You are keeping score for a baseball game with strange rules. The game consists of several rounds, where the scores of past rounds may affect future rounds' scores.

At the beginning of the game, you start with an empty record. You are given a list of strings ops, where ops[i] is the ith operation you must apply to the record and is one of the following:

An integer x - Record a new score of x. "+" - Record a new score that is the sum of the previous two scores. It is guaranteed there will always be two previous scores. "D" - Record a new score that is double the previous score. It is guaranteed there will always be a previous score. "C" - Invalidate the previous score, removing it from the record. It is guaranteed there will always be a previous score. Return the sum of all the scores on the record.

Example:

Input: ops = ["5","2","C","D","+"]
Output: 30
Explanation:
"5" - Add 5 to the record, record is now [5].
"2" - Add 2 to the record, record is now [5, 2].
"C" - Invalidate and remove the previous score, record is now [5].
"D" - Add 2 * 5 = 10 to the record, record is now [5, 10].
"+" - Add 5 + 10 = 15 to the record, record is now [5, 10, 15].
The total sum is 5 + 10 + 15 = 30.

Input: ops = ["5","-2","4","C","D","9","+","+"]
Output: 27
Explanation:
"5" - Add 5 to the record, record is now [5].
"-2" - Add -2 to the record, record is now [5, -2].
"4" - Add 4 to the record, record is now [5, -2, 4].
"C" - Invalidate and remove the previous score, record is now [5, -2].
"D" - Add 2 * -2 = -4 to the record, record is now [5, -2, -4].
"9" - Add 9 to the record, record is now [5, -2, -4, 9].
"+" - Add -4 + 9 = 5 to the record, record is now [5, -2, -4, 9, 5].
"+" - Add 9 + 5 = 14 to the record, record is now [5, -2, -4, 9, 5, 14].
The total sum is 5 + -2 + -4 + 9 + 5 + 14 = 27.

Input: ops = ["1"]
Output: 1

Constraints:

  • 1 <= ops.length <= 1000
  • ops[i] is "C", "D", "+", or a string representing an integer in the range [-3 * 10^4, 3 * 10^4].
  • For operation "+", there will always be at least two previous scores on the record.
  • For operations "C" and "D", there will always be at least one previous score on the record.

思考线


解题思路

这道题的关键是要模拟出+/D/C的操作,对于整数x我们只要保存好数据,然后最后进行相加操作就好。

那么如何模拟出这些计分操作呢? 由于我们要对数据的尾部进行操作,所以我们使用 stack这种数据结构来保存数据。因为栈具有典型的FILO特性。

对于每个操作

  • 整数x:直接 转换为Number,然后push到栈尾
  • +: 获取最末尾两位的得分,然后相加得到的结果push到栈的尾部
  • D: 获取到最末尾的分数,然后 *2,将结果push到栈的尾部
  • C:直接执行pop操作,将栈的尾部推出来。

有了上面的分析,那么整道题的答案就呼之欲出了

/**
 * @param {string[]} ops
 * @return {number}
 */
var calPoints = function (ops) {
    const stack = []
    ops.forEach(item => {
        switch (item) {
            case "+":
                stack.push(stack[stack.length - 1] + stack[stack.length - 2]);
                break;
            case "C":
                stack.pop();
                break;
            case "D":
                stack.push(stack[stack.length - 1] * 2);
                break;
            default:
                stack.push(+item)

        }
    })
    return stack.reduce((a, b) => a + b, 0)

};

这就是我对本题的解法,如果有疑问或者更好的解答方式,欢迎留言互动。