C++, Straightforward Solution


  • 0
    Z
    class Solution {
    public:
        int calPoints(vector<string>& ops) {
            vector<int> scores;
            for (int i = 0; i < ops.size(); i++) {
                if (ops[i] == "+") 
                    scores.push_back(scores.back()+scores[scores.size()-2]);
                else if (ops[i] == "D")
                    scores.push_back(2*scores.back());
                else if (ops[i] == "C")
                    scores.pop_back();
                else
                    scores.push_back(stoi(ops[i]));
            }
            return accumulate(scores.begin(), scores.end(), 0);
        }
    };
    

Log in to reply
 

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