https://www.cnblogs.com/grandyang/p/4452220.html
用迭代的方法, 每次从 3 个字符中选择一个然后传给下一次迭代
- class Solution {
- public:
- vector<string> letterCombinations(string digits) {
- vector<string> output;
- if(digits.empty())
- return output;
- string dict[] = {"","", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
- combination(digits,dict,0,"",output);
- return output;
- }
- void combination(string digits,string dict[],int index,string input,vector<string> &output){
- if(index == digits.size()){
- output.push_back(input);
- return;
- }
- string str = dict[digits[index] - '0'];
- for(int i = 0;i < str.size();i++)
- combination(digits,dict,index+1,input + str[i],output);
- }
- };
来源: http://www.bubuko.com/infodetail-2980998.html