682. Baseball Game: Simple Simulation Problems

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

Topic description

This is a 682. Baseball game on LeetCode on the Easy difficulty .

Tag : "simulation"

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 list of strings that record the operations ops, where O p s [ i ] ops[i] is the number you need to record i i operation,opsfollow the following rules:

  1. Integer x- Indicates new points gained this roundx
  2. "+"- Indicates that the new score obtained in 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.
  3. "D"- Indicates that the new score obtained in this round is twice the previous score. The item data guarantees that a valid score is always present before this action is recorded.
  4. "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 < = O p s . l e n g t h < = 1000 1 <= ops.length <= 1000
  • O p s [ i ] ops[i] "C""D""+",或者一个表示整数的字符串。整数范围是 [ 3 1 0 4 , 3 1 0 4 ] [-3 * 10^4, 3 * 10^4]
  • 对于 "+" 操作,题目数据保证记录此操作时前面总是存在两个有效的分数
  • 对于 "C""D" 操作,题目数据保证记录此操作时前面总是存在一个有效的分数

模拟

根据题意进行模拟即可。

代码:

class Solution {
    static int[] nums = new int[1010];
    public int calPoints(String[] ops) {
        int n = ops.length, idx = 0;
        for (int i = 0; i < n; i++, idx++) {
            if (ops[i].equals("+")) nums[idx] = nums[idx - 1] + nums[idx - 2];    
            else if (ops[i].equals("D")) nums[idx] = nums[idx - 1] * 2;
            else if (ops[i].equals("C")) idx -= 2;
            else nums[idx] = Integer.parseInt(ops[i]);
        }
        int ans = 0;
        for (int i = 0; i < idx; i++) ans += nums[i];
        return ans;
    }
}
复制代码

nums = [0 for _ in range(1010)]
class Solution:
    def calPoints(self, ops: List[str]) -> int:
        idx = 0
        for i in range(len(ops)):
            if ops[i] == '+':
                nums[idx] = nums[idx - 1] + nums[idx - 2]
            elif ops[i] == 'D':
                nums[idx] = nums[idx - 1] * 2
            elif ops[i] == 'C':
                idx -= 2
            else:
                nums[idx] = int(ops[i])
            idx += 1
        return sum(nums[i] for i in range(idx))
复制代码
  • 时间复杂度: O ( n ) O(n)
  • 空间复杂度: O ( n ) O(n)

最后

这是我们「刷穿 LeetCode」系列文章的第 No.682 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。

In this series of articles, in addition to explaining the problem-solving ideas, the most concise code will be given as much as possible. If general solutions are involved, corresponding code templates will also be provided.

In order to facilitate students to debug and submit code on the computer, I have established a related repository: github.com/SharingSour… .

In the warehouse address, you can see the link to the solution of the series of articles, the corresponding code of the series of articles, the link to the original question of LeetCode and other preferred solutions.

Guess you like

Origin juejin.im/post/7079224311206117407