作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/baseball-game/description/

题目描述

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 valid round’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.

Example 1:

Input: ["5","2","C","D","+"]
Output: 30
Explanation:
Round 1: You could get 5 points. The sum is: 5.
Round 2: You could get 2 points. The sum is: 7.
Operation 1: The round 2's data was invalid. The sum is: 5.
Round 3: You could get 10 points (the round 2's data has been removed). The sum is: 15.
Round 4: You could get 5 + 10 = 15 points. The sum is: 30.

Example 2:

Input: ["5","-2","4","C","D","9","+","+"]
Output: 27
Explanation:
Round 1: You could get 5 points. The sum is: 5.
Round 2: You could get -2 points. The sum is: 3.
Round 3: You could get 4 points. The sum is: 7.
Operation 1: The round 3's data is invalid. The sum is: 3.
Round 4: You could get -4 points (the round 3's data has been removed). The sum is: -1.
Round 5: You could get 9 points. The sum is: 8.
Round 6: You could get -4 + 9 = 5 points. The sum is 13.
Round 7: You could get 9 + 5 = 14 points. The sum is 27.

Note:

  1. The size of the input list will be between 1 and 1000.
  2. Every integer represented in the list will be between -30000 and 30000.

题目大意

模拟棒球游戏,游戏规则是:

  1. 如果是数字,那么代表本局得分
  2. 如果是"+",代表本局得分是前两局的和
  3. 如果是"C",代表上一轮的得分是无效的
  4. 如果是"D",代表本轮得分是上一轮的二倍

解题方法

使用栈模拟

这个题貌似困难,其实只要用了一个栈就很简单了。

只需要判断本局的操作,然后对应的移除栈顶、栈顶翻倍、栈顶求和、数字进栈等等。最后对栈的所有元素进行求和。

class Solution(object):
def calPoints(self, ops):
"""
:type ops: List[str]
:rtype: int
"""
valid = []
for op in ops:
if op == 'C':
valid.pop()
elif op == 'D':
valid.append(valid[-1] * 2)
elif op == '+':
valid.append(valid[-1] + valid[-2])
else:
valid.append(int(op))
return sum(valid)

日期

2018 年 1 月 27 日
2018 年 11 月 8 日 —— 项目进展缓慢

【LeetCode】682. Baseball Game 解题报告(Python)的更多相关文章

  1. LeetCode 682 Baseball Game 解题报告

    题目要求 You're now a baseball game point recorder. Given a list of strings, each string can be one of t ...

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

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

  3. LeetCode 1 Two Sum 解题报告

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

  4. 【LeetCode】Permutations II 解题报告

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

  5. 【LeetCode】Island Perimeter 解题报告

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

  6. 【LeetCode】01 Matrix 解题报告

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

  7. 【LeetCode】Largest Number 解题报告

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

  8. 【LeetCode】Gas Station 解题报告

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

  9. LeetCode: Unique Paths II 解题报告

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

随机推荐

  1. illumina SNP 芯片转基因型矩阵

    一.芯片数据 此次拿到的illumina芯片数据并不是原始的数据,已经经过GenomeStudio软件处理成了finalreport文件,格式如下: 之前没处理过芯片数据,对于这种编码模式(Forwa ...

  2. 搭建简单的SpringCloud项目二:服务层和消费层

    GitHub:https://github.com/ownzyuan/test-cloud 前篇:搭建简单的SpringCloud项目一:注册中心和公共层 后篇:搭建简单的SpringCloud项目三 ...

  3. 【NetCore】RabbitMQ 封装

    RabbitMQ 封装 代码 https://gitee.com/wosperry/wosperry-rabbit-mqtest/tree/master 参考Abp事件总线的用法,对拷贝的Demo进行 ...

  4. hadoop运行jar包报错

    执行命令:[root@hadoop102 mapreduce]# hadoop jar mapreduce2_maven.jar Filter 错误信息:Exception in thread &qu ...

  5. js处理title超长问题

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  6. HUD总结

    HUD 指示器/HUD/遮盖/蒙板 半透明的指示器如何实现 指示器的alpha = 1.0; 指示器的背景色是半透明的 1. 创建颜色 直接创建对应的颜色 + (UIColor *)blackColo ...

  7. Spring Boot中使用Dubbo

    高并发下Redis会出现的问题: 缓存穿透 缓存雪崩 热点缓存 一.定义commons工程11-dubboCommons (1) 创建工程 创建Maven的Java工程,并命名为11-dubboCom ...

  8. Ajax异步更新网页(使用原生JavaScript)

    一.页面代码 <!DOCTYPE html> <html> <head> <title>MyHtml.html</title> <me ...

  9. 【C/C++】习题3-2 分子量/算法竞赛入门经典/字符串

    给出一种物质的分子式,求分子量.只包含4种原子,分别为C,H,O,N. [知识点] 1.ASCII码 [阿拉伯数字]48~57 [大写字母]65~90 [小写字母]97~122 2.输入循环到n-1的 ...

  10. 论文翻译:2021_A Perceptually Motivated Approach for Low-complexity, Real-time Enhancement of Fullband Speech

    论文地址:一种低复杂度实时增强全频带语音的感知激励方法 论文代码 引用格式:A Perceptually Motivated Approach for Low-complexity, Real-tim ...