[LeetCode&Python] Problem 682. Baseball Game
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了[LeetCode&Python] Problem 682. Baseball Game,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2263字,纯文字阅读大概需要4分钟。
内容图文
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 682. Baseball Game所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。