Recursion version in C language without additional library

  • 0

    I try to solve the problem without additional library ,e.g. Glibc's queue.

     * Return an array of size *returnSize.
     * Note: The returned array must be malloced, assume caller calls free().
    char *map[]={"0","1","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
    void f(char *d, int ri, int *rcnt, char **res, char *tmp){
        if(d[ri] == '\0'){
            tmp[ri] = '\0';
            char *t = malloc((strlen(d)+1)*sizeof(char));
            strcpy(t, tmp);
            res[*rcnt] = t;
            *rcnt += 1;
        for(int i=0; i<strlen(map[d[ri]-'0']);i++){
            tmp[ri] = map[d[ri]-'0'][i];
            f(d, ri+1, rcnt, res, tmp);
    char** letterCombinations(char* digits, int* returnSize) {
        int total_comb = 1;
            return 0;
        for(int i=0; i< strlen(digits);i++){
            total_comb *= strlen(map[digits[i]-'0']);
        char **res = (char**)malloc(total_comb * sizeof(char*)); 
        int ri=0, rcnt=0;
        char *tmp = malloc((strlen(digits)+1)*sizeof(char));
        f(digits, ri, &rcnt, res, tmp);
        *returnSize = total_comb;
        return res;

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.