LeetCode682. Baseball Game
来源:互联网 发布:淘宝昵称在哪里显示 编辑:程序博客网 时间:2024/06/06 00:37
LeetCode682. 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: 30Explanation: 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: 27Explanation: 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:
题目分析:一开始的时候小胖脚一下,所以wa了,但是把这道题和vector类型的时候就很简单了,于是这道题就不多做分析了。
代码:
class Solution {public:int calPoints(vector<string>& ops) {int sum = 0, top = -1;vector<int> k;for (auto i : ops) {if (i == "+") {sum += k[top] + k[top - 1];k.push_back(k[top] + k[top - 1]);top++;}else if (i == "D") {sum += k[top] * 2;k.push_back(k[top] * 2);top++;}else if (i == "C") {sum -= k[top];k.pop_back();top--;}else {k.push_back(stoi(i));sum += stoi(i);top++;}}return sum;}};
阅读全文
0 0
- python--leetcode682. Baseball Game
- LeetCode682. Baseball Game
- Baseball Game
- LeeCode- Baseball Game
- [LeetCode] 682. Baseball Game
- leetcode 682. Baseball Game
- 682. Baseball Game
- <LeetCode>682. Baseball Game
- Leetcode. 682. Baseball Game
- 682. Baseball Game
- 682. Baseball Game
- Baseball Game(leetcode)
- LeetCode.682 Baseball Game
- 【LeetCode】682. Baseball Game
- 682. Baseball Game
- 682. Baseball Game
- 682. Baseball Game(vector..)
- LC-Baseball Game
- 洛谷P1364 医院设置(floyd)
- 10/1/2017 今天开通博客
- 每日一诗词 —— 沁园春·长沙
- i8绿色版升级解决qq管家报错问题
- Python2 和 Python3 中默认编码的差异
- LeetCode682. Baseball Game
- Mac下破解百度云不限速
- Algorithms: Bit Manipulation(位操作)
- MessageBox 函数
- 中国天气网城市代码爬取
- LeetCode653. Two Sum IV
- 5.广义线性回归
- MATLAB R2016a 在 Windows 安装和破解
- LeetCode125. Valid Palindrome