-
Notifications
You must be signed in to change notification settings - Fork 0
/
BASEBALL_GAME.cpp
49 lines (46 loc) · 1.28 KB
/
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
class Solution {
public:
int calPoints(vector<string>& ops) {
stack<int> points;
int sum = 0;
int sz = ops.size();
int i,j,temp1,temp2;
int cur;
for(i=0;i<sz;i++)
{
temp1 = 0,temp2 = 0;
if(ops[i]=="+"){
if(!points.empty()){
temp1 = points.top();
points.pop();
if(!points.empty()){
temp2 = points.top();
}
points.push(temp1);
}
points.push(temp1+temp2);
sum+=(temp1+temp2);
}
else if(ops[i]=="D"){
if(!points.empty()){
temp1 = points.top();
temp1*=2;
}
points.push(temp1);
sum+=temp1;
}
else if(ops[i]=="C"){
if(!points.empty()){
temp1 = points.top();
points.pop();
}
sum-=temp1;
}
else{
sum+=stoi(ops[i]);
points.push(stoi(ops[i]));
}
}
return sum;
}
};