-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbaseball_game.cpp
44 lines (44 loc) · 1006 Bytes
/
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
class Solution {
public:
int calPoints(vector<string>& ops) {
int sum = 0;
stack <int> st;
int top_ele;
for(int i=0;i<ops.size();i++)
{
if(ops[i]=="C")
{
if(!st.empty())
{
st.pop();
}
}
else if(ops[i]=="D")
{
top_ele = st.top();
st.push(top_ele*2);
}
else if(ops[i]=="+")
{
if(!st.empty())
{
top_ele = st.top();
st.pop();
int el2 = st.top();
st.push(top_ele);
st.push(top_ele+el2);
}
}
else
{
st.push(stoi(ops[i]));
}
}
while(!st.empty())
{
sum += st.top();
st.pop();
}
return sum;
}
};