还是典型的栈应用
注意string到int的转换就可以啦
class Solution {
public:
int calPoints(vector<string>& ops) {
stack<int> stk;
int sum = 0;
for(const auto &c : ops){
if(c == "+"){
if(stk.size() > 1){
int num = stk.top(); stk.pop();
int tmp = num + stk.top();
stk.push(num);
stk.push(tmp);
}
}else if(c == "D"){
if(!stk.empty())
stk.push(2 * stk.top());
}else if(c == "C"){
if(!stk.empty())
stk.pop();
}else{
stk.push(stoi(c));
}
}
while(!stk.empty()){
sum += stk.top();
stk.pop();
}
return sum;
}
};