首页 > 其他 > 详细

LeetCode 682 Baseball Game 解题报告

时间:2019-02-23 15:25:19      阅读:158      评论:0      收藏:0      [点我收藏+]

题目要求

You‘re now a baseball game point recorder.

Given a list of strings, each string can be one of the 4 following types:

  1. Integer (one round‘s score): Directly represents the number of points you get in this round.
  2. "+" (one round‘s score): Represents that the points you get in this round are the sum of the last two valid round‘s points.
  3. "D" (one round‘s score): Represents that the points you get in this round are the doubled data of the last valid round‘s points.
  4. "C" (an operation, which isn‘t a round‘s score): Represents the last validround‘s points you get were invalid and should be removed.

Each round‘s operation is permanent and could have an impact on the round before and the round after.

You need to return the sum of the points you could get in all the rounds.

题目分析及思路

题目规定了四种字符:整数字符即是这一轮的所得分数,‘+’表示该轮分数是前两轮有效分数的和,‘D’表示该轮分数是前一轮有效分数的翻倍,‘C’表示上轮分数无效。最后给出所有轮数结束后的总得分。可以遍历每个字符,对每个字符做条件判断,将有效分数存在一个列表里。最后返回列表的和。

python代码

class Solution:

    def calPoints(self, ops: List[str]) -> int:

        res = []

        for op in ops:

            if op == ‘C‘:

                res.pop()

            elif op == ‘+‘:

                res.append(res[-1] + res[-2])

            elif op == ‘D‘:

                res.append(2 * res[-1])

            else:

                res.append(int(op))

        return sum(res)

                

                

            

        

 

LeetCode 682 Baseball Game 解题报告

原文:https://www.cnblogs.com/yao1996/p/10422700.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!