//这个玩意和第59题很像的,做这个只需要模拟即可,洞开开辟一个数组来存储,从左到右遍历,再将元素赋值,就ok了(建议去看leetcode的官方题解,我菜)
int* spiralOrder(int** matrix, int matrixSize, int* matrixColSize, int* returnSize){
if (matrixSize == 0 || matrixColSize[0] == 0) {
*returnSize = 0;
return NULL;
}
//跟59好像差不多,试试模拟
//我是这么想的,用几个for循环,然后分别取出数值,放入到一个一维数组里
int rows = matrixSize, columns = matrixColSize[0];
int total = rows * columns;
int* order = malloc(sizeof(int) * total);
*returnSize = 0;
int left = 0, right = columns - 1, top = 0, bottom = rows - 1;//建议画个图,就是转圈圈
while (left <= right && top <= bottom) {
for (int column = left; column <= right; column++) {
order[(*returnSize)++] = matrix[top][column];
}
for (int row = top + 1; row <= bottom; row++) {
order[(*returnSize)++] = matrix[row][right];
}
if (left < right && top < bottom) {
for (int column = right - 1; column > left; column--) {
order[(*returnSize)++] = matrix[bottom][column];
}
for (int row = bottom; row > top; row--) {
order[(*returnSize)++] = matrix[row][left];
}
}
left++;
right--;
top++;
bottom--;
}
return order;
}