-
Notifications
You must be signed in to change notification settings - Fork 119
/
54.c
78 lines (68 loc) · 1.93 KB
/
54.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <stdio.h>
#include <stdlib.h>
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int *spiralOrder(int **matrix, int matrixRowSize, int matrixColSize) {
int *ret = (int *)malloc(matrixRowSize * matrixColSize * sizeof(int));
if (matrix == NULL || matrixRowSize == 0 || matrixColSize == 0)
return ret;
int left = 0;
int right = matrixColSize - 1;
int up = 0;
int down = matrixRowSize - 1;
int k = 0;
int i;
while (left <= right && up <= down) {
if (left == right && up == down) {
ret[k++] = matrix[left][up];
}
else if (left == right) {
for (i = up; i <= down; i++)
ret[k++] = matrix[i][right];
}
else if (up == down) {
for (i = left; i <= right; i++)
ret[k++] = matrix[up][i];
}
else {
for (i = left; i <= right - 1; i++)
ret[k++] = matrix[up][i];
for (i = up; i <= down - 1; i++)
ret[k++] = matrix[i][right];
for (i = right; i >= left + 1; i--)
ret[k++] = matrix[down][i];
for (i = down; i >= up + 1; i--)
ret[k++] = matrix[i][left];
}
left++;
right--;
up++;
down--;
}
return ret;
}
int main() {
int rows = 4;
int cols = 3;
int **matrix = (int **)malloc(rows * sizeof(int *));
int i, j;
for (i = 0; i < rows; i++) {
matrix[i] = (int *)malloc(cols * sizeof(int));
}
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
matrix[i][j] = i * cols + j + 1;
printf("%d ", matrix[i][j]);
}
printf("\n");
}
printf("------\n");
int *ret = spiralOrder(matrix, rows, cols);
int size = rows * cols;
for (i = 0; i < size; i++) {
printf("%d ", ret[i]);
}
printf("\n");
return 0;
}