[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 ...
随机推荐
- JavaScript 基础知识入门
js3种弹出框 alert消息对话框 var mychar=I love JavaScript;alert(mychar); confirm消息对话框 返回值:bool var mymessage ...
- 12月21日 简单理解Active Recore Callback, destroy_all和delete_all的区别。豆知识(alias),语言学习法(4核心)
destroy_all and delete_all Destroy the records by instantiating each record and calling its #destroy ...
- python-day33--进程间通信(IPC)
方式:队列(推荐使用) 一.基本情况 1.可以往队列里放任意类型的数据 2. 队列:先进先出 3. q=Queue(3) #可以设置队列中最多可以进入多少个值,也可以不设置 q.put('first' ...
- HDU-4849 Wow! Such City! (单源最短路)
Problem Description Doge, tired of being a popular image on internet, is considering moving to anoth ...
- PL/SQL 中 dbms_output.put_line 输出字符长度限制的问题
可以使用dbms_out.enable()函数来设定允许的长度. PL/SQL 中 dbms_output.put_line 输出字符长度限制的问题
- OC 文件基本操作
// // main.m // oc_13 // // Created by ma c on 15/12/17. // Copyright (c) 2015年 博文科技. All rights res ...
- OC BLOCK和协议
一.BOLCK (一)简介 BLOCK是什么?苹果推荐的类型,效率高,在运行中保存代码.用来封装和保存代码,有点像函数,BLOCK可以在任何时候执行. block实际上是: 指向结构体的指针 BOLC ...
- MySQL解析过程、执行过程
转载:https://student-lp.iteye.com/blog/2152601 https://www.cnblogs.com/cdf-opensource-007/p/6502556.ht ...
- ES profile 性能优化用——返回各个shard的耗时
Profile API 都说要致富先修路,要调优当然需要先监控啦,elasticsearch在很多层面都提供了stats方便你来监控调优,但是还不够,其实很多情况下查询速度慢很大一部分原因是糟糕的查询 ...
- learning uboot switch to standby system using button
pseudocode: If(reset_button was pressed ) { Change uboot env bootslot^1 }