-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0682-baseball-game.cpp
41 lines (30 loc) · 981 Bytes
/
0682-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
class Solution {
public:
int calPoints(vector<string>& ops) {
stack<int> stack;
int sum = 0;
for (int i = 0; i < ops.size(); i++){
if (ops[i] == "+"){
int first = stack.top();
stack.pop();
int second = stack.top();
stack.push(first);
stack.push(first + second);
sum += first + second;
}
else if (ops[i] == "D"){
sum += 2 * stack.top();
stack.push(2 * stack.top());
}
else if (ops[i] == "C"){
sum -= stack.top();
stack.pop();
}
else{
sum += stoi(ops[i]);
stack.push(stoi(ops[i]));
}
}
return sum;
}
};