forked from lzl124631x/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths1.cpp
More file actions
21 lines (21 loc) · 648 Bytes
/
Copy paths1.cpp
File metadata and controls
21 lines (21 loc) · 648 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// OJ: https://leetcode.com/problems/baseball-game/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(N)
class Solution {
public:
int calPoints(vector<string>& ops) {
vector<int> v;
for (auto op : ops) {
if (isdigit(op[0]) || op[0] == '-') v.push_back(stoi(op));
else {
switch(op[0]) {
case 'C': v.pop_back(); break;
case 'D': v.push_back(v.back() * 2); break;
case '+': v.push_back(v.back() + v[v.size() - 2]); break;
}
}
}
return accumulate(v.begin(), v.end(), 0);
}
};