fix: minor simplify

This commit is contained in:
eat-swap 2022-04-12 21:45:25 +08:00
parent 386bc59f72
commit 991f2962bd
No known key found for this signature in database
GPG Key ID: 8C089CB1A2B7544F
1 changed files with 2 additions and 7 deletions

View File

@ -22,10 +22,9 @@ public:
static int calPoints(std::vector<std::string>& ops) { static int calPoints(std::vector<std::string>& ops) {
std::vector<int> scores; std::vector<int> scores;
std::for_each(ops.begin(), ops.end(), [&](const std::string& str) { std::for_each(ops.begin(), ops.end(), [&](const std::string& str) {
int x;
try { try {
x = std::stoi(str); scores.push_back(std::stoi(str));
} catch (const std::invalid_argument& ignored) { } catch (std::invalid_argument) {
switch (str[0]) { switch (str[0]) {
case '+': case '+':
scores.push_back(*std::prev(scores.end(), 2) + scores.back()); scores.push_back(*std::prev(scores.end(), 2) + scores.back());
@ -35,12 +34,8 @@ public:
break; break;
case 'D': case 'D':
scores.push_back(2 * scores.back()); scores.push_back(2 * scores.back());
break;
default:;
} }
return;
} }
scores.push_back(x);
}); });
return std::accumulate(scores.begin(), scores.end(), 0); return std::accumulate(scores.begin(), scores.end(), 0);
} }