Leetcode 682. Baseball Game

1080 단어 Algorithm
글 쓴 이: Tyan 블 로그: noahsnail. com |  CSDN  |  약서
1. Description
2. Solution
class Solution {
public:
    int calPoints(vector& ops) {
        int score = 0;
        int sum = 0;
        vector scores;
        for(int i = 0; i < ops.size(); i++) {
            if(ops[i] == "C") {
                sum -= scores.back();
                scores.pop_back();
            }
            else if (ops[i] == "D") {
                score = 2 * scores.back();
                sum += score;
                scores.push_back(score);
            }
            else if ((ops[i] == "+")) {
                score = scores.back() + scores[scores.size() - 2];
                sum += score;
                scores.push_back(score);
            }
            else {
                score = stoi(ops[i]);
                sum += score;
                scores.push_back(score);
            }
        }
        return sum;
    }
};

Reference
  • https://leetcode.com/problems/baseball-game/description/
  • 좋은 웹페이지 즐겨찾기