682. Baseball Game
我的思路:
这题目没有什么难度,只需要对着题目模拟一下就好了
我的代码:
写的丑求轻喷
class Solution {
public:
vector<int> valids;
int res;
int calPoints(vector<string>& ops) {
valids.clear();
res = 0;
int len = ops.size();
int cnt;
for(int i = 0; i < len; i++) {
if(ops[i] == "+") {
cnt = valids.size();
int t1 = valids[cnt-1];
int t2 = valids[cnt-2];
valids.push_back(t1 + t2);
res += t1 + t2;
debug();
} else if(ops[i] == "D") {
cnt = valids.size();
int t = valids[cnt-1] * 2;
valids.push_back(t);
res += t;
debug();
} else if(ops[i] == "C") {
cnt = valids.size();
res -= valids[cnt-1];
valids.pop_back();
debug();
} else {
int tmp = stoi(ops[i]);
valids.push_back(tmp);
res += tmp;
debug();
}
}
return res;
}
int stoi(string s) {
stringstream ss;
ss << s;
int result;
ss >> result;
return result;
}
void debug() {
// int len = valids.size();
// for(int i = 0; i < len; i++) {
// cout << valids[i] << " ";
// }
// cout << endl;
// cout << "res " << res << endl;
}
};