# LeetCode: 566. 重塑矩阵¶

## 1、题目描述¶

输入:
nums =
[[1,2],
[3,4]]
r = 1, c = 4

[[1,2,3,4]]



输入:
nums =
[[1,2],
[3,4]]
r = 2, c = 4

[[1,2],
[3,4]]



1. 给定矩阵的宽和高范围在 [1, 100]。
2. 给定的 r 和 c 都是正数。

## 2、解题思路¶

​ 实际思路很简单，创建了矩阵以后，从头开始赋值，

​ 从第一行的第一个元素开始数

/**
* 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) {
// columnSizes = (int **) malloc(sizeof(int *));
if (numsRowSize * numsColSize != r * c) {
*returnSize = numsRowSize;
*columnSizes = (int *) malloc(sizeof(int) * numsRowSize);
for (int i = 0; i < numsRowSize; i++) {
(*columnSizes)[i] = numsColSize;
}
return nums;
}

*returnSize = r;
*columnSizes = (int *) malloc(sizeof(int) * r);
for (int i = 0; i < r; i++) {
(*columnSizes)[i] = c;
}

int **result = (int **) malloc(sizeof(int *) * r);
int *line;
for (int i = 0; i < r * c; i++) {
if (i % c == 0) {
line = (int *) malloc(sizeof(int) * c);
result[i / c] = line;
}
result[i / c][i % c] = nums[i / numsColSize][i % numsColSize];
}

return result;
}