/**
* Return an array of arrays of size *returnSize.
* The sizes of the arrays are returned as *columnSizes array.
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
*/
int** matrixReshape(int** nums, int numsRowSize, int numsColSize, int r, int c, int** columnSizes, int* returnSize) {
int ** result;
int i, j,a=0,b=0;
if (numsRowSize*numsColSize != r*c)
{
r = numsRowSize;
c = numsColSize;
}
*returnSize = r;
result = (int **)malloc(r * sizeof(int *));
*columnSizes = (int *)malloc(r * sizeof(int));
for (int i = 0; i < r; i++) {
result[i] = (int *)malloc(c * sizeof(int));
(*columnSizes)[i] = c;
}
for(i = 0; i < numsRowSize; i++)
{
for (j = 0; j < numsColSize; j++)
{
result[a][b++] = nums[i][j];
if (b == c)
{
b = 0;
a++;
}
}
}
return result;
}