---
# System prepended metadata

title: LeetCode - 682. Baseball Game
tags: [LeetCode]

---

---
title: LeetCode - 682. Baseball Game
tags: LeetCode
description: by cpp
---
# [LeetCode] 682. Baseball Game
input : vector<string>& ops 
* interger 把數字加至尾端
* '+' 末兩個相加，加回尾端    
* 'D' 最後一個雙倍，加回尾端
* 'C' 刪除尾端

output : 把處理後的整個陣列相加

```cpp=

class Solution {
public:
    int calPoints(vector<string>& ops) {
        //宣告要在switch外面宣告
        vector<int> content;
        int result=0;
        int back =0;
        int sum=0;
        auto riter= content.rbegin();
        //用迭代方式遍歷ops
        for(auto iter= ops.begin();iter!=ops.end();iter++){
            //.data()這個方法用來把string_basic<char> 轉型到char
            char c = *iter->data();
            switch(c){
                case '+':
                    //從後面迭代兩個回去
                    sum = 0;
                    riter= content.rbegin();
                    sum+= *riter;
                    riter++;
                    sum+= *riter;
                    content.push_back(sum);
                    break;
                case 'D':
                    back = content.back();
                    //content.pop_back();
                    content.push_back(back*2);
                    break;
                case 'C':
                    content.pop_back();
                    break;
                default:
                    //用stoi()來把字串回傳成數字
                    //x: 不能用ASCII 這樣遇到負數會出問題
                    content.push_back(stoi(*iter));
                    break;
            }
                 
        }

        for(int i=0;i<content.size();i++){
            result+=content[i];
        } 
        return result;
    }
};
```

{%hackmd theme-dark %}