summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-05-21 13:45:23 +0800
committercrupest <crupest@outlook.com>2020-05-21 13:45:23 +0800
commitaee7cb0b0c15ecb364e811a2230337f90b870ff4 (patch)
tree4032f0856dfd290bb82981d22fcb7ea9462bb7d7
parentbeb3c36432f57ecc20236081385f8adfd8a52081 (diff)
downloadsolutions-aee7cb0b0c15ecb364e811a2230337f90b870ff4.tar.gz
solutions-aee7cb0b0c15ecb364e811a2230337f90b870ff4.tar.bz2
solutions-aee7cb0b0c15ecb364e811a2230337f90b870ff4.zip
Add problem 17 .
-rw-r--r--cpp/17.cpp56
1 files changed, 56 insertions, 0 deletions
diff --git a/cpp/17.cpp b/cpp/17.cpp
new file mode 100644
index 0000000..74e33b4
--- /dev/null
+++ b/cpp/17.cpp
@@ -0,0 +1,56 @@
+#include <string>
+#include <vector>
+
+using std::string;
+using std::vector;
+
+vector<char> c_map[9]{
+ {'a', 'b', 'c'},
+ {'d', 'e', 'f'},
+ {'g', 'h', 'i'},
+ {'j', 'k', 'l'},
+ {'m', 'n', 'o'},
+ {'p', 'q', 'r', 's'},
+ {'t', 'u', 'v'},
+ {'w', 'x', 'y', 'z'}};
+
+void combine(const string::const_iterator &current, const string::const_iterator &end, string &head, vector<string> &result)
+{
+ const auto &chars = c_map[(*current) - '2'];
+
+ if (current == end)
+ {
+ for (auto c : chars)
+ {
+ head.push_back(c);
+ result.push_back(head);
+ head.pop_back();
+ }
+ return;
+ }
+
+ for (auto c : chars)
+ {
+ head.push_back(c);
+ combine(current + 1, end, head, result);
+ head.pop_back();
+ }
+}
+
+class Solution
+{
+public:
+ vector<string> letterCombinations(string digits)
+ {
+ std::vector<string> result;
+
+ if (digits.empty())
+ return result;
+
+ std::string head;
+
+ combine(digits.cbegin(), digits.cend() - 1, head, result);
+
+ return result;
+ }
+};