###### [leetcode-54]Spiral Matrix(C语言)

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.

For example,
Given the following matrix:

[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
You should return [1,2,3,6,9,8,7,4,5].

int* spiralOrder(int** matrix, int matrixRowSize, int matrixColSize) {
int length = matrixRowSize*matrixColSize;
int *result = (int *)malloc(sizeof(int)*length);
char *visited = (char *)calloc(length,sizeof(char));//store visited element

int index = 0;
int row=0,col=-1;
while(1){
if(index == length)
break;
for(col=col+1;col<matrixColSize;col++){
if(visited[row*matrixColSize+col] == 1)
break;
result[index++]=matrix[row][col];
visited[row*matrixColSize+col] = 1;
}
col--;
for(row = row+1;row<matrixRowSize;row++){
if(visited[row*matrixColSize+col] == 1)
break;
result[index++] = matrix[row][col];
visited[row*matrixColSize+col] = 1;
}
row--;
for(col = col-1;col>=0;col--){
if(visited[row*matrixColSize+col] == 1)
break;
result[index++] = matrix[row][col];
visited[row*matrixColSize+col] = 1;
}
col++;
for(row = row-1;row>=0;row--){
if(visited[row*matrixColSize+col] == 1)
break;
result[index++] = matrix[row][col];
visited[row*matrixColSize+col] = 1;
}
row++;
}
return result;
}

#### 剑指offer 面试题20：顺时针打印矩阵及其变形(LeetCode54. Spiral Matrix旋转矩阵) 题解

2016-05-13 13:04:42

#### [C语言][LeetCode][54]Spiral Matrix

2016-12-13 10:24:52

#### LeetCode-54-Spiral Matrix(C语言实现)

2018-01-03 06:12:17

#### Spiral Matrix

2014-03-13 19:44:03

#### <em>C语言</em>写的旋转方阵问题

2018年04月22日 00:00

#### 54. Spiral Matrix

2018-04-17 14:44:35

#### 54. Spiral Matrix (重要！！！)

2016-07-17 23:01:15

#### 54. Spiral Matrix 【M】【82】

2016-06-06 15:19:07

#### leetcode_c++： Spiral Matrix II（059）

2016-06-01 00:28:33

#### [LeetCode]59.Spiral Matrix II

2015-02-04 22:25:02

## 不良信息举报

[leetcode-54]Spiral Matrix(C语言)