682. 棒球比赛
题目链接:682. 棒球比赛
代码如下:
class Solution
{
public:
int calPoints(vector<string>& operations)
{
vector<int> scores;
for(int i=0;i<operations.size();i++)
{
if(operations[i]=="C")
{
scores.pop_back();
}
else if(operations[i]=="D")
{
scores.push_back(scores[scores.size()-1]*2);
}
else if(operations[i]=="+")
{
scores.push_back(scores[scores.size()-1]+scores[scores.size()-2]);
}
else
{
scores.push_back(atoi(operations[i].c_str()));
}
}
int res=accumulate(scores.begin(),scores.end(),0);
return res;
}
};