Given a digit string, return all possible letter combinations that the number could represent.
A mapping of digit to letters (just like on the telephone buttons) is given below.
![]()
Input:Digit string "23" Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.
void findletters(string digits,int index,char* letters[],vector<string> &res,string &tmp,int tmpindex)
{
while(index < digits.length() && (digits[index] == '0' || digits[index] == '1'))
++index;
if(index >= digits.length()) {res.push_back(tmp);return;}
for(int i=0;letters[digits[index]-'0'][i] != '\0'; i++){
if(tmp.length() == tmpindex)
tmp += letters[digits[index]-'0'][i];
else
tmp[tmpindex] = letters[digits[index]-'0'][i];
findletters(digits, index+1, letters, res, tmp, tmpindex+1);
}
}
vector<string> letterCombinations(string digits) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
char* letters[10] ={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
vector<string> res;
int len = digits.length();
if(len < 1){res.push_back(""); return res;}
int index = 0;
while((digits[index] == '0' || digits[index] == '1') && index < len)
++index;
if(index >= len) {res.push_back(""); return res;}
for(int i=0;letters[digits[index]-'0'][i] != '\0'; i++){
string tmp = "";
tmp += letters[digits[index]-'0'][i];
int tmpindex = 0;
findletters(digits, index+1, letters, res, tmp, tmpindex+1);
}
}

给定一个数字字符串,返回所有可能的字母组合。提供了一个数字到字母的映射,如下所示。注意,虽然上面的答案是按字典序排列的,但你的答案可以是任意顺序。

3680

被折叠的 条评论
为什么被折叠?



