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:

  • 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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. [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 ...

  5. [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 ...

  6. [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 ...

  7. [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, ...

  8. [LeetCode&Python] Problem 1: Two Sum

    Problem Description: Given an array of integers, return indices of the two numbers such that they ad ...

  9. [LeetCode&Python] Problem 811. Subdomain Visit Count

    A website domain like "discuss.leetcode.com" consists of various subdomains. At the top le ...

随机推荐

  1. McAfee 与 360使用感受

    运维给配的 win 10.4G内存 ,装的 McAfee,每次开机啥事不干内存去了55%, 开齐qq.微信.vscode.浏览器就要冲90%,再多开几个网页电脑就卡得很, 最近,卡死机了.. 然后看了 ...

  2. Spring AMQP 源码分析 07 - MessageListenerAdapter

    ### 准备 ## 目标 了解 Spring AMQP 如何用 POJO 处理消息 ## 前置知识 <Spring AMQP 源码分析 04 - MessageListener> ## 相 ...

  3. C# DataTable按指定列排序

    C#提供的内置对象DataTable功能特别的强大,如果我们需要对DataTable中的某一列进行排序怎么处理呢,具体代码如下: DataTable dt = new DataTable(); dt. ...

  4. CSS实现和选择器

    CSS实现和选择器 本课内容: 一.实现CSS四种方式 1,每个html标签中都有一个style样式属性,该属性的值就是css代码.(针对一个标签)2,使用style标签的方式. 一般都定义在head ...

  5. linux编译安装mysql5.1.x

    安装mysql,安装前准备 如果mysql用户不存在,那么添加mysql用户 groupadd mysql useradd -g mysql mysql mysql编译安装 make时间特别长 wge ...

  6. 『TensorFlow』项目资源分享

    TF中文社区 TF_GOOGLE官方代码学习 1.TensorFlow-Slim TF-Slim 是 tensorflow 较新版本的扩充包,可以简化繁杂的网络定义,其中也提供了一些demo: Ale ...

  7. nyoj-833-博弈

    833-取石子(七) 内存限制:64MB 时间限制:1000ms 特判: No通过数:16 提交数:30 难度:1 题目描述: Yougth和Hrdv玩一个游戏,拿出n个石子摆成一圈,Yougth和H ...

  8. Leetcode 105

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  9. Leetcode 784

    //这代码可真丑陋,但我学到了两点1:char和string可以无缝互相转换2:char可以直接加减数字进行转换string不行 class Solution { public: vector< ...

  10. ** exception error: no match of right hand side value

    错误发生的情况是模式匹配失败.对于badmatch异常,很难找到单一的原因,但经常性的原因是你无意间尝试绑定已绑定过的变量.