class Solution {
public:
int calPoints(vector<string>& ops) {
int ans = 0;
vector<int> stk;
int len = ops.size();
int p = 0;//始终指向栈的末尾
for(int i = 0;i<len;i++) {
if(ops[i]!="+"&&ops[i]!="D"&&ops[i]!="C") {
stk.push_back(stoi(ops[i]));
}
else {
switch(ops[i][0]) {//字符串string转成字符char ch
case '+':stk.push_back(stk[p]+stk[p-1]);break;
case 'D':stk.push_back(2*stk[p]);break;
case 'C':stk.pop_back();break;
}
}
p = stk.size()-1;
}
while(!stk.empty()) {
ans+=stk.back();
stk.pop_back();
}
return ans;
}
};
LeetCode 682. 棒球比赛 题解 C/C++
于 2021-05-01 20:38:25 首次发布