-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseballGame.cpp
More file actions
30 lines (29 loc) · 821 Bytes
/
BaseballGame.cpp
File metadata and controls
30 lines (29 loc) · 821 Bytes
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
class Solution {
public:
int calPoints(vector<string>& ops) {
if (ops.empty()) return 0;
stack<int> st;
int res = 0;
for (int i=0; i<ops.size(); i++) {
if (ops[i] == "C") {
res -= st.top();
st.pop();
} else if (ops[i] == "D") {
st.push(st.top()*2);
res += st.top();
} else if (ops[i] == "+") {
int val1 = st.top();
st.pop();
int val2 = st.top();
st.push(val1);
res += (val1+val2);
st.push(val1+val2);
} else {
int x = stoi(ops[i]);
res += x;
st.push(x);
}
}
return res;
}
};