Evaluation
topic
leetcode 17
given a numeric string containing only 2-9, it can return all letter combinations indicated.
Given digital map to letters as follows (the same telephone key). Note 1 does not correspond to any alphabet.
Example:
Input: "23"
outputs:. [ "Ad", " ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]
Description:
Although the above answers are arranged in order according to the dictionary, but you can choose the order of the answer output.
void dfs(char *digits, char *str, int ind, char letter[][5], char **res, int *returnSize) {
if (digits[ind] == '\0') {
str[ind] = '\0';
strcpy(res[*returnSize], str);
(*returnSize)++;//谨记*的符号低于++,记得加括号
return ;
}
int d = digits[ind] - '0', i = 0;
while (letter[d][i] != '\0') {
str[ind] = letter[d][i];
dfs(digits, str, ind + 1, letter, res, returnSize);
i++;
}
}
char ** letterCombinations(char * digits, int* returnSize){
*returnSize = 0;
if (digits[0] == '\0') return digits;
char letter[][5] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
int len = strlen(digits);
int count = 1;
for (int i = 0; i < len; i++) {
count *= 4;
}
char **res = (char **)malloc(sizeof(char *) * count);
for (int i = 0; i < count; i++) {
res[i] = (char *)malloc(sizeof(char) * (len + 1));
res[i][len] = '\0';
}
char *str = (char *)malloc(sizeof(char) * (len + 1));
dfs(digits, str, 0, letter, res, returnSize);
return res;
}