-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy path054-Spiral Matrix.c
34 lines (34 loc) · 1.04 KB
/
054-Spiral Matrix.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* spiralOrder(int** matrix, int matrixRowSize, int matrixColSize) {
int *res = malloc((matrixRowSize*matrixColSize) * sizeof(int));
int c = 0;
int startM = 0, endM = matrixRowSize - 1, startN = 0, endN = matrixColSize - 1;
int i, j;
if (matrixRowSize <= 0 || matrixColSize <= 0) {
return res;
}
while ((startM <= endM) && (startN <= endN)) {
for (i = startN ; i <= endN ; i++) {
res[c++] = matrix[startM][i];
}
for (i = startM + 1 ; i <= endM ; i++) {
res[c++] = matrix[i][endN];
}
// make sure endM is still available
if (startM == endM) break;
for (i = endN - 1 ; i >= startN ; i--) {
res[c++] = matrix[endM][i];
}
if (startN == endN) break;
for (i = endM - 1 ; i >= startM + 1 ; i--) {
res[c++] = matrix[i][startN];
}
startN++;
startM++;
endN--;
endM--;
}
return res;
}