-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbaseball_game.cpp
More file actions
31 lines (28 loc) 路 808 Bytes
/
Copy pathbaseball_game.cpp
File metadata and controls
31 lines (28 loc) 路 808 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
31
// https://leetcode.com/problems/baseball-game/
class Solution {
public:
int calPoints(vector<string>& ops) {
int sum = 0;
stack<int> stk;
for(int i = 0; i < ops.size(); i++) {
if (ops[i] == "+") {
int tmp = stk.top();
stk.pop();
int tmp2 = stk.top();
stk.push(tmp);
stk.push(tmp+tmp2);
sum += tmp + tmp2;
} else if(ops[i] == "D") {
sum += 2*stk.top();
stk.push(2*stk.top());
}else if(ops[i] == "C") {
sum -= stk.top();
stk.pop();
} else {
sum += stoi(ops[i]);
stk.push(stoi(ops[i]));
}
}
return sum;
}
};