-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbaseball_game.cpp
29 lines (27 loc) · 980 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
//problem link: https://leetcode.com/problems/baseball-game/
class Solution {
public:
int calPoints(vector<string>& operations) {
vector<int> v;
for (int i = 0; i < operations.size(); i++) {
if (operations[i] != "C" && operations[i] != "D" && operations[i] != "+") {
v.push_back(stoi(operations[i]));
}
else if (operations[i] == "C") {
if (!v.empty()) v.pop_back();
}
else if (operations[i] == "D") {
if (!v.empty()) v.push_back(v.back() * 2);
else v.push_back(0);
}
else if (operations[i] == "+") {
if (v.empty()) v.push_back(0);
else if (v.size() == 1) v.push_back(v.back());
else v.push_back(v[v.size() - 1] + v[v.size() - 2]);
}
}
int sum = 0;
for (int i = 0; i < v.size(); i++) sum += v[i];
return sum;
}
};