Share my c code


  • 1
    K

    int** generate(int numRows, int** columnSizes, int* returnSize) {

    int **res_array = (int**)malloc(sizeof(int*) * numRows);
    
    *columnSizes = (int*)malloc(sizeof(int) * numRows);
    for (int i = 1; i <= numRows; i++) {
        int *cur, *pre, idx = i - 1;
    
        res_array[idx] = (int*)malloc(sizeof(int) * i);
        cur = res_array[idx];
        pre = res_array[idx - 1];
        
        for (int j = 0; j < i; j++) {
            if (j == 0) cur[j] = 1;
            else if (j == i - 1) cur[j] = 1;
            else cur[j] = pre[j] + pre[j - 1];
        }
        
        (*columnSizes)[idx] = i;
    }
    
    *returnSize = numRows;
    return res_array;
    

    }


Log in to reply
 

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