C++ simple and straight-forward solution


  • 0
    M
        int calPoints(vector<string>& ops) {
            stack<int> s;
            for(int i=0;i<ops.size();i++) {
                if(ops[i]=="+") {
                    if(s.empty()) continue;
                    int score=s.top(), last=score;
                    s.pop();
                    if(!s.empty()) score+=s.top();
                    s.push(last);
                    s.push(score);
                }
                else if(ops[i]=="C") {
                    if(!s.empty()) s.pop();
                }
                else if(ops[i]=="D") {
                    if(!s.empty()) s.push(s.top()*2);
                }
                else s.push(stoi(ops[i]));
            }
            int res=0;
            while(!s.empty()) {
                res+=s.top();
                s.pop();
            }
            return res;
        }

Log in to reply
 

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