#17 Letter Combinations of a Phone Number

题目链接:https://leetcode.com/problems/letter-combinations-of-a-phone-number/

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.

/**
 * Return an array of size *returnSize.
 * Note: The returned array must be malloced, assume caller calls free().
 */
void SubCombination(char* digits, char map[][5], char* output, char** ret, int* returnSize, int k) {    //k表示递归到当前字母的下标
    int i = 0;
    if(digits[k] == '') {     //基准情况;递归到字符串结束处。将当前输
        output[k] = '';
        if(k)
            strcpy(ret[(*returnSize)++], output);       //复制字符串到输出数组
        return;
    }
    while(map[digits[k] -  '0'][i]) { //当前数字相应的每个字母进行递归
        output[k] = map[digits[k] - '0'][i];
        SubCombination(digits, map, output, ret, returnSize, k + 1);
        ++i;
    }
}
char** letterCombinations(char* digits, int* returnSize) {
    *returnSize = 0;
    char map[10][5] = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
    char output[20] = {};       //输出字符串
    char **ret = (char **)malloc(sizeof(char *) * 1000);
    for(int i = 0; i < 1000; ++i)
        ret[i] = (char *)malloc(sizeof(char) * 20);
    SubCombination(digits, map, output, ret, returnSize, 0);    //从第一个字母開始递归保存每个数字相应字母,到结尾处保存相应字符串
    return ret;
}


原文地址:https://www.cnblogs.com/zsychanpin/p/7086798.html