diff --git a/solutions/17. Letter Combinations of a Phone Number.md b/solutions/17. Letter Combinations of a Phone Number.md index fbf662e..fe75164 100644 --- a/solutions/17. Letter Combinations of a Phone Number.md +++ b/solutions/17. Letter Combinations of a Phone Number.md @@ -53,5 +53,26 @@ public: ``` ## 思路二 -见[此处](https://leetcode.com/problems/letter-combinations-of-a-phone-number/discuss/8454/My-C%2B%2B-solution-use-DFS). -有时间了再自己实现一下。 +``` C++ +class Solution { +private: + const vectordigit2char{"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; + void DFS(const string &digits, string out, vector &res){ + int start = out.size(); + if(start == digits.size()){ + res.push_back(out); + return; + } + for(char c: digit2char[digits[start]-'0']) + DFS(digits, out+c, res); + } +public: + vector letterCombinations(string digits) { + vectorres; + if(digits.size() == 0) return res; + string out; + DFS(digits, out, res); + return res; + } +}; +```