-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathBaseball Game.cpp
32 lines (32 loc) · 925 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
// Runtime: 11 ms (Top 21.91%) | Memory: 8.5 MB (Top 29.43%)
class Solution {
public:
int calPoints(vector<string>& ops) {
stack<int>st;
int n = ops.size();
for(int i=0;i<n;i++){
if(ops[i] == "C"){
st.pop();
}
else if (ops[i] =="D"){
st.push(st.top() * 2);
}
else if(ops[i] =="+"){
int temp = st.top();
st.pop();
int temp2 = st.top();
st.push(temp);
st.push(temp+temp2);
}
else{
st.push(stoi(ops[i]));
}
}
int res = 0;
while(!st.empty()){
res += st.top();
st.pop();
}
return res;
}
};