-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path682. Baseball Game.cpp
56 lines (51 loc) · 1.42 KB
/
682. Baseball Game.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
int calPoints(vector<string>& ops) {
int sum=0;
vector<int>record;
int previous = 0;
for(int i=0;i<ops.size();i++)
{
if (ops[i]=="C")
{
if(record.empty()==false)
{
record.pop_back();
}
}
else if (ops[i]=="D")
{
if(record.empty()==false)
{
record.push_back(2*record[record.size()-1]);
}
}
else if (ops[i]=="+")
{
if(record.size()>=2)
{
record.push_back(record[record.size()-1]+record[record.size()-2]);
}
}
else
{
record.push_back(stoi(ops[i]));
if(record.size()>=2) previous = record[record.size()-2];
}
}
for (int i=0;i<record.size();i++)
{
sum+=record[i];
}
return sum;
}
};
int main(){
Solution A;
vector<string> ops = {"5","-2","4","C","D","9","+","+"};
cout << A.calPoints(ops);
system("pause");
}