class Solution {
public:
    int calPoints(vector<string>& ops) {
        stack<int> ST;
        int sum = 0;
        for (auto o : ops)
        {
            if (o == "C")
            {
                int n = ST.top();
                sum -= n;
                ST.pop();
            }
            else if (o == "D")
            {
                int n = ST.top() * 2;
                sum += n;
                ST.push(n);
            }
            else if (o == "+")
            {
                int t1 = ST.top();
                ST.pop();
                int t2 = ST.top();
                int n = t1 + t2;
                sum += n;
                ST.push(t1);
                ST.push(n);
            }
            else
            {
                int n = atoi(o.c_str());
                sum += n;
                ST.push(n);
            }
        }
        return sum;
    }
};

 

posted on 2018-09-28 18:27  Sempron2800+  阅读(86)  评论(0编辑  收藏  举报