[LeetCode&Python] Problem 682. Baseball Game
You're now a baseball game point recorder.
Given a list of strings, each string can be one of the 4 following types:
Integer
(one round's score): Directly represents the number of points you get in this round."+"
(one round's score): Represents that the points you get in this round are the sum of the last twovalid
round's points."D"
(one round's score): Represents that the points you get in this round are the doubled data of the lastvalid
round's points."C"
(an operation, which isn't a round's score): Represents the lastvalid
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:
- The size of the input list will be between 1 and 1000.
- Every integer represented in the list will be between -30000 and 30000.
Just use a stack to solve this problem.
class Solution:
def calPoints(self, ops):
"""
:type ops: List[str]
:rtype: int
"""
ans=0
baseballstack=collections.deque() for c in ops:
if c!='C' and c!='D' and c!='+':
num=int(c)
ans+=num
baseballstack.appendleft(num)
elif c=='C':
num=baseballstack.popleft()
ans-=num
elif c=='D':
num=baseballstack[0]
num*=2
ans+=num
baseballstack.appendleft(num)
elif c=='+':
num=baseballstack[0]
num2=baseballstack[1]
num+=num2
ans+=num
baseballstack.appendleft(num) return ans
[LeetCode&Python] Problem 682. Baseball Game的更多相关文章
- [LeetCode&Python] Problem 108. Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...
- [LeetCode&Python] Problem 387. First Unique Character in a String
Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex ...
- [LeetCode&Python] Problem 427. Construct Quad Tree
We want to use quad trees to store an N x N boolean grid. Each cell in the grid can only be true or ...
- [LeetCode&Python] Problem 371. Sum of Two Integers
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...
- [LeetCode&Python] Problem 520. Detect Capital
Given a word, you need to judge whether the usage of capitals in it is right or not. We define the u ...
- [LeetCode&Python] Problem 226. Invert Binary Tree
Invert a binary tree. Example: Input: 4 / \ 2 7 / \ / \ 1 3 6 9 Output: 4 / \ 7 2 / \ / \ 9 6 3 1 Tr ...
- [LeetCode&Python] Problem 905: Sort Array By Parity
Given an array A of non-negative integers, return an array consisting of all the even elements of A, ...
- [LeetCode&Python] Problem 1: Two Sum
Problem Description: Given an array of integers, return indices of the two numbers such that they ad ...
- [LeetCode&Python] Problem 811. Subdomain Visit Count
A website domain like "discuss.leetcode.com" consists of various subdomains. At the top le ...
随机推荐
- yii CFormModel中的rules验证机制
public function rules() { return array( array('username, password', 'required'), array('rememberMe', ...
- eclipse jsp:useBean搞死人了。
eclipse jsp:useBean搞死人了 首页,用eclipse需要经常重启tomcat服务器,这是因为你编辑了页面正浏览这个页面,而这个页面还处在之前的错误编译中... 其次,第一次用到use ...
- 手把手教你开发jquery插件(三)
First, i want to add options to Tabs constructor like this: var tabs = $("div.tabs").tabs( ...
- jQuery 插件运行机制和 $冲突解决
1.jQuery.fn.extend(object) 基本插件假设我们要创建一个插件,使一组元素中的文本变为绿色.我们要做的就是添加一个名为greenify的函数, $.fn 将像其他任何jquery ...
- skill prefix neo,non input 1
1● neo 新的 2● non 不,非,无
- PHP与MYSQL数据库链接方法
<?php //Mysqli链接数据库的方法 $host='localhost';//主机地址 $dbname='mydata2017';//数据库名 $username='root';//用户 ...
- html frameset的介绍
frameset 元素可以定义一个框架集.它被用来组织多个窗口(框架).每个框架存有独立的文档. 属性 ①border 设置框架的边框粗细. ②bordercolor 设置框架的边框颜色. ③fram ...
- SQL Server 调优系列进阶篇 - 如何维护数据库索引
前言 上一篇我们研究了如何利用索引在数据库里面调优,简要的介绍了索引的原理,更重要的分析了如何选择索引以及索引的利弊项,有兴趣的可以点击查看. 本篇延续上一篇的内容,继续分析索引这块,侧重索引项的日常 ...
- Nexcel的行列,和单元格坐标
book.Sheets[1].UsedRange.Rows.Count 行数从1开始 book.Sheets[1].UsedRange.LastCol 从0开始 book.Sheets[1].Cell ...
- Java——File类成员方法
body, table{font-family: 微软雅黑} table{border-collapse: collapse; border: solid gray; border-width: 2p ...