반응형

https://leetcode.com/problems/baseball-game/submissions/

 

Baseball Game - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

주어진 연산에 알맞게 수행해주면 되었습니다.

class Solution {
public:
    int calPoints(vector<string>& ops) {
        int sum = 0;
        vector<int> scores;
        for(string& op : ops) {
            if(op == "C") {
                sum -= scores.back();
                scores.pop_back();
            } else {
                scores.push_back(get(scores, op));
                sum += scores.back();
            }
        }
        return sum;
    }
    
    int get(vector<int>& scores, string& op) {
            if(op == "+") return scores.back() + *(scores.rbegin() + 1);
            else if(op == "D") return scores.back() * 2;
            else return stoi(op);
    }
};

 

반응형

'Algorithm' 카테고리의 다른 글

LeetCode 59 : Spiral Matrix II  (0) 2022.04.13
LeetCode 289 : Game of Life  (0) 2022.04.12
LeetCode 37 : Sudoku Solver  (0) 2022.04.09
LeetCode 36 : Valid Sudoku  (0) 2022.04.09
LeetCode 347 : Top K Frequent Elements  (0) 2022.04.09

+ Recent posts