Day 29 - Baseball Game

大家好,我是毛毛。ヾ(´∀ ˋ)ノ
废话不多说开始今天的解题Day~


682. Baseball Game

Question

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:

  1. An integer x - Record a new score of x.
  2. "+" - Record a new score that is the sum of the previous two scores. It is guaranteed there will always be two previous scores.
  3. "D" - Record a new score that is double the previous score. It is guaranteed there will always be a previous score.
  4. "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

Example1

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.

Example2

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.

Example3

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"的话,则是把前一笔记录下来的数字乘上2,再记录成下一笔;"C"的话,则是把前一笔记录下来的数字删掉;"+""D""C"执行的时候都会确保前面有足够的数字可以做运算。最後,回传记录下来的阵列的数字总和。

Think

作法大致上是这样

  • 就照着题目的规则去用if判断式处理,但是因为isnumeric()只能判断0~9之间的String,所以多用了一个if来判断有没有-号,有的话,把minus_flag设成True,进到记录数字的判断式一样直接记录下来。
  • 最後回传记录阵列的数字总和。

Code

Python

class Solution:
    def calPoints(self, ops: List[str]) -> int:
        record = []
        record_index = 0
        minus_flag = False
        
        for index in range(len(ops)):
            if ops[index][0] == "-":
                minus_flag = True
                
            if ops[index].isnumeric() or minus_flag:
                record.append(int(ops[index]))
                minus_flag = False
                record_index += 1
                
            elif ops[index] == "+":
                record.append((int(record[record_index-2])+int(record[record_index-1])))
                record_index += 1
                
            elif ops[index] == "D":
                record.append((int(record[record_index-1])*2))
                record_index += 1
                
            elif ops[index] == "C":
                record.pop(-1)
                record_index -= 1

        return sum(record)

Result

  • Python

大家明天见/images/emoticon/emoticon29.gif


<<:  团队共生

>>:  <Day29> 实战!!投资小白的出击!!!!

结语 - 相关的展望

感想 第三十天,来点结语好了,非常感谢 IT 邦帮忙这举办的铁人活动,尤其是平常上班,没有特别的动力...

Day3

做好学习程序语言这件事,可以说已经成为了全民运动。在人类的历史中,我们总是尽了一切努力想搞懂学习程序...

Day 10 Azure cognitive service: image description- 看图说故事

Azure cognitive service: image description- 看图说故事 ...

DevOps在MLOps当中的角色

我们在前面的写给MLOps人才培育苦手谈论到MLOps是一个需要大家通力合作的一项专案,除了该专案带...

[Lesson24] Kotlin - 条件

if-else Kotlin的条件判断叙述比较特别,它能够用 if-else 赋值给变数 val r...