还是像上篇说的那样,学 cpp 一段时间,但是有好多东西不会呀,leetcode 的这些题目也不算难,但基础不牢很尴尬呀。
题目还是不贴了好了,毕竟有人搜题目的话肯定是写到题目的人,so
class Solution {
public:
int calPoints(vector<string>& ops) {
int num = 0;
vector<int> last;
for(auto &ch:ops)
{
if (ch == "C")
{
last.pop_back();
}
else if (ch == "D")
{
last.push_back(last.at(last.size() - 1) * 2);
}
else if (ch == "+")
{
last.push_back(last.at(last.size() - 1) + last.at(last.size() - 2));
}
else
{
last.push_back(stoi(ch));
}
}
for (auto& n : last)
num += n;
return num;
}
};
学了点 vector 的使用吧,.pop_back, .push_back, .at…