Stack-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.
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的更多相关文章
- 【Leetcode_easy】682. Baseball Game
problem 682. Baseball Game solution: 没想到使用vector! class Solution { public: int calPoints(vector<s ...
- [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 ...
- 682. Baseball Game 棒球游戏 按字母处理
[抄题]: You're now a baseball game point recorder. Given a list of strings, each string can be one of ...
- [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 ...
- LeetCode 682 Baseball Game 解题报告
题目要求 You're now a baseball game point recorder. Given a list of strings, each string can be one of t ...
- 【LeetCode】682. Baseball Game 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用栈模拟 日期 题目地址:https://leet ...
- 682. Baseball Game
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
- 682. Baseball Game (5月28日)
解答(打败98.60%) class Solution { public: int calPoints(vector<string>& ops) { vector<int&g ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- leetcode 学习心得 (4)
645. Set Mismatch The set S originally contains numbers from 1 to n. But unfortunately, due to the d ...
随机推荐
- layui学习<一>
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- DNA motif 搜索算法总结
DNA motif 搜索算法总结 2011-09-15 ~ ADMIN 翻译自:A survey of DNA motif finding algorithms, Modan K Das et. al ...
- OAuth 2.0 学习
OAuth是一个关于授权(authorization)的开放网络标准,在全世界得到广泛应用,目前的版本是2.0版. 本文对OAuth 2.0的设计思路和运行流程,做一个简明通俗的解释,主要参考材料为R ...
- $.post 提示错误: Uncaught SyntaxError: Unexpected token :
$.post("addRecommond",{"productId":productId,"categoryCode":categoryCo ...
- 在EF中使用MySQL的方法及常见问题
有时需要在网上租用空间或数据库,Mysql成本低一些,所以想将sql server转成mysql…… 注意:在安装Mysql时要选择文字集为utf8,否则将不能使用中文(当前也可以在创建数据库时使用u ...
- android 网站上下的 adt 不能显示没有安装的
问题描述 使用SDK Manager更新时出现问题Failed to fetch URL https://dl-ssl.google.com/android/repository/repository ...
- hdu-1147(跨立实验)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1147 思路:判断每条线段,如果将要输入的线段和已经有的线段相交,则这条线段不算. 参考文章:https ...
- Django入门指南-第9章:静态文件设置(完结)
http://127.0.0.1:8000 #下一步是告诉Django在哪里可以找到静态文件.打开settings.py,拉到文件的底部,在STATIC_URL后面添加以下内容: STATICFILE ...
- 一组RS485设备操作命令
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ZNJM ...
- HDU 2546 饭卡 (01背包问题)
题意:中文的吧,飘过~ 析:学过DP的都应该感觉到是动态规划吧,就是一个01背包问题,不同的是,这个题又加入一些新的条件,就是不满5元不能消费,过了5元即使超了也行(这个学校真不错,都可以预支),最后 ...