【leetcode】17. 电话号码的字母组合

 

void recursion(char * digits, int* returnSize,char** arr,char** map,int cur,char* s,int len){
    if (cur >= len){
        arr[(*returnSize)] = (char*)calloc(len + 1, sizeof(char));
        strcat(arr[(*returnSize)++], s);
        return;
    }
    for (int i = 0; i < strlen(map[digits[cur] - '0']); i++){
        s[cur] = map[digits[cur] - '0'][i];
        recursion(digits, returnSize, arr, map, cur + 1, s, len);
        s[cur] = '\0';
    }
}
char ** letterCombinations(char * digits, int* returnSize){    
    char* map[] = { "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };
    int i, j, len=strlen(digits);
    *returnSize = 0;
    if (len==0) 
        return NULL;
    char* s = (char*)calloc(len+1, sizeof(char));
    char** arr = (char**)calloc(1000, sizeof(char*));
    recursion(digits, returnSize, arr, map, 0, s,len);
    return arr;
}

 

上一篇:【leetcode】将每个元素替换为右侧最大元素


下一篇:两个链表合并