[682. Baseball Game] C++_Stack


  • 0
    class Solution {
    public:
    int calPoints(vector<string>& ops) {
        stack<int> vailds;
        int res = 0;
        for(auto s : ops){
            if(s == "D"){
                int score = 2*(vailds.top());
                vailds.push(score);
                res += score;
            }else if(s == "C"){
                int score = vailds.top();
                vailds.pop();
                res -= score;
            }else if(s == "+"){
                int score1 = vailds.top(); vailds.pop();
                int score2 = vailds.top(); vailds.push(score1);
                int score = score1 + score2; vailds.push(score);
                res += score;
            }else{
                int score = stoi(s);
                vailds.push(score);
                res += score;
            }
        }
        return res;
    }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.