LeetCode第 682 题:棒球比赛(C++)

    技术2025-09-25  97

    682. 棒球比赛 - 力扣(LeetCode)

    还是典型的栈应用

    注意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; } };
    Processed: 0.008, SQL: 9