【Leetcode_easy】682. Baseball Game

problem

682. Baseball Game

solution:

没想到使用vector!

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

参考

1. Leetcode_easy_682. Baseball Game;

2. Grandyang;

猜你喜欢

转载自www.cnblogs.com/happyamyhope/p/11091366.html