Skip to content

Instantly share code, notes, and snippets.

@zhangxiaomu01
Created October 26, 2018 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhangxiaomu01/7a1fe9ba79c4c818b9c91a2eee5143ca to your computer and use it in GitHub Desktop.
Save zhangxiaomu01/7a1fe9ba79c4c818b9c91a2eee5143ca to your computer and use it in GitHub Desktop.
class Solution {
private:
vector<string> dic_s = {"", "", "abc", "def", "ghi","jkl", "mno","pqrs", "tuv", "wxyz"};
vector<string> result;
public:
void dfs(const string &digits, string s, int pos){
int len = digits.size();
if(pos >= len){
return;
}
int subLen = dic_s[digits[pos] - '0'].size();
for(int i = 0; i< subLen; i++)
{
string dic_string = dic_s[digits[pos] - '0'];
string temp = s + dic_string[i];
if(pos == len - 1)
{
result.push_back(temp);
}
dfs(digits, temp, pos + 1);
}
}
vector<string> letterCombinations(string digits) {
dfs(digits, "", 0);
return result;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment