题解 | #螺旋矩阵#
螺旋矩阵
http://www.nowcoder.com/practice/7edf70f2d29c4b599693dc3aaeea1d31
/**
*
* @param matrix int整型二维数组
* @param matrixRowLen int matrix数组行数
* @param matrixColLen int* matrix数组列数
* @return int整型一维数组
* @return int* returnSize 返回数组行数
*/
int* spiralOrder(int** matrix, int matrixRowLen, int* matrixColLen, int* returnSize ) {
// write code here
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;
}