程式語言 - LeetCode - C - 注意事項 - Array



二維記憶體配置方式

int** flipAndInvertImage(int **image, int imageSize, int *imageColSize, int *returnSize, int **returnColumnSizes)
{
    int cc = 0;
    int **rbuf = NULL;

    // 回傳Buffer
    rbuf = malloc(sizeof(int *) * imageSize);
    for (cc = 0; cc < imageSize; cc++) {
        rbuf[cc] = malloc(sizeof(int) * imageColSize[cc]);
    }

    // 回傳Buffer的長度
    *returnSize = imageSize;

    // 回傳Buffer的二維長度
    *returnColumnSizes = malloc(sizeof(int) * imageSize);
    for (cc = 0; cc < imageSize; cc++) {
        (*returnColumnSizes)[cc] = imageColSize[cc];
    }

    return rbuf;
}

P.S. 只有returnSize不需要配置記憶體