682. 棒球比赛
模拟栈,so easy 对叭
class Solution {
public:
int tt = 0;
int s[1010] = {0};
int calPoints(vector<string>& ops) {
for (auto& c : ops) {
if (c == "+") {
s[tt + 1] = s[tt] + s[tt - 1];
tt ++ ;
}
else if (c == "D") {
s[tt + 1] = s[tt] * 2;
tt ++ ;
}
else if (c == "C") {
tt -- ;
}
else s[ ++ tt] = stoi(c);
}
int res = 0;
for (int i = 1; i <= tt; i ++ ) {
res += s[i];
}
return res;
}
};
本文介绍了一种使用模拟栈实现的棒球比赛计分算法。通过解析一系列操作指令,如加分、加倍、取消等,该算法能准确计算出当前比赛的总得分。采用数组作为模拟栈的数据结构,并通过索引来模拟栈顶操作。
383

被折叠的 条评论
为什么被折叠?



