int** matrixReshape(int** mat, int matSize, int* matColSize, int r, int c, int* returnSize, int** returnColumnSizes)
{
// 条件不满足,返回原矩阵
if (r * c != matSize * (*matColSize)) {
*returnSize = matSize;
*returnColumnSizes = malloc(sizeof(int) * matSize);
for (int i = 0; i < matSize; i++) {
(*returnColumnSizes)[i] = matColSize[i];
}
return mat;
}
// 先把2维矩阵,转成1维
int *arrayOne = malloc(sizeof(int) * r * c);
for (int i = 0, count = 0; i < matSize; i++) {
for (int j = 0; j < *matColSize; j++) {
arrayOne[count++] = mat[i][j];
}
}
// 再把1维转成需要的2维矩阵
// 方法:int**本质就是指向int*的指针,让它指向1维数组的不用部位即可
int **res = malloc(sizeof(int*) * r);
*returnSize = r;
*returnColumnSizes = malloc(sizeof(int) * r);
for (int i = 0; i < r; i++) {
(*returnColumnSizes)[i] = c;
// 开始换皮,把1维转成最终的2维
res[i] = arrayOne + i*c;
}
return res;
}
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* spiralOrder(int** matrix, int matrixSize, int* matrixColSize, int* returnSize)
{
if(matrixSize==0){
*returnSize = 0;
return NULL;
}
int *res = malloc(sizeof(int) * matrixSize * (*matrixColSize));
*returnSize = matrixSize * (*matrixColSize);
int m = 0; // 行数
int n = 0; // 列数
int top = 0;
int bottom = matrixSize - 1;
int left = 0;
int right = (*matrixColSize) - 1;
/*
* direction = 0, right
* direction = 1, down/bottom
* direction = 2, left
* direction = 3, up/top
**/
int direction = 0;
for (int i = 0; i < matrixSize * (*matrixColSize); i++) {
switch(direction) {
case 0:
res[i] = matrix[m][n];
// n == right
if (n == right) {
direction = 1;
m++; // 下移
top++; // 边界下移
} else {
n++;
}
break;
case 1:
res[i] = matrix[m][n];
// m == bottom
if (m == bottom) {
direction = 2;
n--; // 左移
right--; // 边界左移
} else {
m++;
}
break;
case 2:
res[i] = matrix[m][n];
// n == left
if (n == left) {
direction = 3;
m--;
bottom--;
} else {
n--;
}
break;
case 3:
res[i] = matrix[m][n];
// m == top
if (m == top) {
direction = 0;
n++;
left++;
} else {
m--;
}
break;
default:
break;
}
}
return res;
}
/**
* Return an array of arrays of size *returnSize.
* The sizes of the arrays are returned as *returnColumnSizes array.
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
*/
enum {
RIGHT,
DOWN,
LEFT,
UP
};
int** generateMatrix(int n, int* returnSize, int** returnColumnSizes)
{
// 申请内存和初始化
*returnSize = n;
*returnColumnSizes = malloc(sizeof(int) * n);
int **res = malloc(sizeof(int*) * n);
for (int i = 0; i < n; i++) {
res[i] = malloc(sizeof(int) * n);
memset(res[i], 0, sizeof(int) * n);
(*returnColumnSizes)[i] = n;
}
int direction = RIGHT;
int row = 0, col = 0;
int top = 0, bottom = n - 1, left = 0, right = n - 1;
for (int num = 1; num <= n * n; num++) {
res[row][col] = num;
switch(direction) {
case RIGHT:
if (col == right) {
direction = DOWN;
top++;
row++;
} else {
col++;
}
break;
case DOWN:
if (row == bottom) {
direction = LEFT;
right--;
col--;
} else {
row++;
}
break;
case LEFT:
if (col == left) {
direction = UP;
bottom--;
row--;
} else {
col--;
}
break;
case UP:
if (row == top) {
direction = RIGHT;
left++;
col++;
} else {
row--;
}
break;
}
}
return res;
}