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.
class Solution {
public:
int getNum(string& s) {
int num;
stringstream ss;
ss << s;
ss >> num;
return num;
}
int calPoints(vector<string>& ops) {
int sum = ;
vector<int> round;
for (int i = ; i < ops.size(); i++) {
if (ops[i] == "C") {
sum -= round.back();
round.pop_back();
} else if (ops[i] == "D") {
int temp = * round.back();
round.push_back(temp);
sum += temp;
} else if (ops[i] == "+") {
int index = round.size() - ;
int temp = round[index] + round[index-];
round.push_back(temp);
sum += temp;
} else {
int temp = getNum(ops[i]);
sum += temp;
round.push_back(temp);
}
}
return sum;
}
};

Stack-682. Baseball Game的更多相关文章

  1. 【Leetcode_easy】682. Baseball Game

    problem 682. Baseball Game solution: 没想到使用vector! class Solution { public: int calPoints(vector<s ...

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

  3. 682. Baseball Game 棒球游戏 按字母处理

    [抄题]: You're now a baseball game point recorder. Given a list of strings, each string can be one of ...

  4. [LeetCode] 682. Baseball Game 棒球游戏

    You're now a baseball game point recorder. Given a list of strings, each string can be one of the 4 ...

  5. LeetCode 682 Baseball Game 解题报告

    题目要求 You're now a baseball game point recorder. Given a list of strings, each string can be one of t ...

  6. 【LeetCode】682. Baseball Game 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用栈模拟 日期 题目地址:https://leet ...

  7. 682. Baseball Game

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

  8. 682. Baseball Game (5月28日)

    解答(打败98.60%) class Solution { public: int calPoints(vector<string>& ops) { vector<int&g ...

  9. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

  10. leetcode 学习心得 (4)

    645. Set Mismatch The set S originally contains numbers from 1 to n. But unfortunately, due to the d ...

随机推荐

  1. 关于UI设计的一些工作了解

    关于UI设计相信大家在刚接触UI的时候都不太了解,我来说说我在一段学习时间后的了解. UI从工作内容上来说分为3大类,即研究工具,研究人与界面的关系,研究人与之相应. ​ UI设计师的职能一个是图形设 ...

  2. jsp 页面 摘要, 要截取字符串 ,当时 字符串中包含 html标签,截取后无法显示

    如题: 处理办法: 1.  使用struts标签 <s:property  value ="#text.replaceAll('<[^>]+>','').substr ...

  3. windows10 装linux子系统

    http://blog.csdn.net/Yuxin_Liu/article/details/52347898 试了一下,下载太慢,就没继续用,可以用实验楼这个网来玩玩linux

  4. Windows Server 2012如何实现双网卡绑定

    在windows server 2012 之前我们在服务器上如果要实现双网卡绑定则需要向服务器厂家所要相应的软件,但是现在强大的windows server 2012的到来使我们省去了所有的麻烦,因为 ...

  5. 2018.09.24 bzoj1016: [JSOI2008]最小生成树计数(并查集+搜索)

    传送门 正解是并查集+矩阵树定理. 但由于数据范围小搜索也可以过. 我们需要知道最小生成树的两个性质: 不同的最小生成树中,每种权值的边出现的个数是确定的 不同的生成树中,某一种权值的边连接完成后,形 ...

  6. 2018.07.03 HDU Rikka with Phi(线段树)

    Rikka with Phi Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) P ...

  7. 2018.09.21 atcoder An Invisible Hand(贪心)

    传送门 简单贪心啊. 这题显然跟t并没有关系,取差量最大的几组买入卖出就行了. 于是我们统计一下有几组差量是最大的就行了. 代码: #include<bits/stdc++.h> #def ...

  8. hdu-1251(string+map)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 思路:重点是用gets输入,而且用a[20],不能直接输入string类型的. #include ...

  9. 表格边框重复合并属性: border-collapse:collapse;

    table { border-collapse:collapse; }

  10. LA 4329 Ping pong (树状数组)

    题意:从左到右给你n个不同的数值,让你找出三个数值满足中间的数值在两边的数值之间的个数. 析:题意还是比较好理解的,关键是怎么求数量,首先我们分解一下只有两种情况,一个是左边<中间<右边, ...