此篇文章為我的解題紀錄,程式碼或許並不是很完善

Leetcode - 682. Baseball Game

解題思路

利用vector的pop_back()、push_back()和back() function來處理對應到CD或是+的問題

我滴程式碼

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
class Solution {
public:
int calPoints(vector<string>& ops) {
vector<int> sum;
for (int i = 0; i < ops.size(); i++)
{
if(ops[i] == "C")
{
sum.pop_back();
}
else if (ops[i] == "D")
{
sum.push_back(sum.back() * 2);
}
else if (ops[i] == "+")
{
sum.push_back(sum[sum.size() - 1] + sum[sum.size() -2]);
}
else
{
sum.push_back(stoi(ops[i]));
}
}
int total = 0;
for (int i = 0; i < sum.size(); i++)
{
total += sum[i];
}
return total;
}
};