diff options
author | terminaldweller <thabogre@gmail.com> | 2022-04-10 14:48:23 +0000 |
---|---|---|
committer | terminaldweller <thabogre@gmail.com> | 2022-04-10 14:48:23 +0000 |
commit | 67e340b5da56ca45671d2afc7efc7a081a871ef3 (patch) | |
tree | a2cdf2b96da861fa70e0a8f5370651f8bc7821ea /682/main.cpp | |
parent | update (diff) | |
download | leetcode-67e340b5da56ca45671d2afc7efc7a081a871ef3.tar.gz leetcode-67e340b5da56ca45671d2afc7efc7a081a871ef3.zip |
update
Diffstat (limited to '682/main.cpp')
-rw-r--r-- | 682/main.cpp | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/682/main.cpp b/682/main.cpp new file mode 100644 index 0000000..25432b8 --- /dev/null +++ b/682/main.cpp @@ -0,0 +1,40 @@ + +#include "header.hpp" +#include <string> + +class Solution { +public: + static int calPoints(std::vector<std::string> &ops) { + std::vector<int> result; + result.reserve(1000); + int sum = 0; + for (auto &iter : ops) { + if (iter == "+") { + auto dummy = result[result.size() - 1] + result[result.size() - 2]; + result.push_back(dummy); + sum += dummy; + } else if (iter == "D") { + auto dummy = result[result.size() - 1] * 2; + result.push_back(dummy); + sum += dummy; + } else if (iter == "C") { + sum -= result[result.size() - 1]; + result.pop_back(); + } else { + auto dummy = std::stoi(iter); + result.push_back(dummy); + sum += dummy; + } + } + + return sum; + } +}; + +int main(int argc, char **argv) { + std::vector<std::string> ops1 = {"5", "2", "C", "D", "+"}; + std::vector<std::string> ops2 = {"5", "-2", "4", "C", "D", "9", "+", "+"}; + std::cout << Solution::calPoints(ops1) << "\n"; + std::cout << Solution::calPoints(ops2) << "\n"; + return 0; +} |