给定一个m x n大小的矩阵(m行,n列),按螺旋的顺序返回矩阵中的所有元素。
matrixRowLen int matrix数组行数
matrixColLen int* matrix数组列数
int* spiralOrder(int** matrix, int matrixRowLen, int* matrixColLen, int* returnSize ) {
int s=0,x=matrixRowLen-1,z=0,y=*matrixColLen-1;//s = 上,x = 下,z = 左,y = 右
int len = matrixRowLen * *matrixColLen;
int* arr = (int*)malloc(sizeof(int)*len);
int count = 0;
while(1)
{
for(int i=z;i<=y;i++)//从左到右
{
arr[count++] = matrix[s][i];
}
if(count >= len) break;
for(int i=s+1;i<x;i++)//从上到下
{
arr[count++] = matrix[i][y];
}
if(count >= len) break;
for(int i=y;i>=z;i--)//从右到左
{
arr[count++] = matrix[x][i];
}
if(count >= len) break;
for(int i=x-1;i>s;i--)//从下到上
{
arr[count++] = matrix[i][z];
}
if(count >= len) break;
s++;
x--;
z++;
y--;
}
*returnSize = len;
return arr;
}