Letter Combinations of a Phone NumberJan 27 '124852 / 14644
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"].
这题有点诡异,如果没有res.clear(),vector中会多一个“”。
char m[10][5] = {" ","", "abc","def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
class Solution {
public:
vector<string> res;
int len;
vector<string> letterCombinations(string digits) {
len = digits.size();
char *c = new char[len+1];
c[len] = '\0';
res.clear();
gen(digits, 0, c);
return res;
}
void gen(const string& num, int cur, char *c) {
if (cur == len) {
res.push_back(c);
return;
}
char *s = &(m[num[cur] - '0'][0]);
while (*s != '\0') {
c[cur] = *s;
gen(num, cur+1, c);
s++;
}
}
};

本文介绍了一种算法,用于根据电话按键上的字母映射,找出所有可能的字母组合。输入为一串数字字符,输出所有可能的字符串组合。通过递归方式实现,每次选择一个数字对应的字母加入到当前组合。
546

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



