forked from begeekmyfriend/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: begeekmyfriend <[email protected]>
- Loading branch information
1 parent
afc05f0
commit eebb0bd
Showing
4 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
all: | ||
gcc -O2 -o test spiral_matrix.c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
/** | ||
** Note: The returned array must be malloced, assume caller calls free(). | ||
**/ | ||
static int* spiralOrder(int** matrix, int matrixRowSize, int matrixColSize) | ||
{ | ||
int hor_top = 0; | ||
int hor_bottom = matrixRowSize - 1; | ||
int ver_left = 0; | ||
int ver_right = matrixColSize - 1; | ||
int *nums = malloc(matrixRowSize * matrixColSize * sizeof(int)); | ||
int count = 0; | ||
int i, direction = 0; | ||
|
||
while (hor_top <= hor_bottom && ver_left <= ver_right) { | ||
switch (direction) { | ||
case 0: | ||
for (i = ver_left; i <= ver_right; i++) { | ||
nums[count++] = matrix[hor_top][i]; | ||
} | ||
hor_top++; | ||
break; | ||
case 1: | ||
for (i = hor_top; i <= hor_bottom; i++) { | ||
nums[count++] = matrix[i][ver_right]; | ||
} | ||
ver_right--; | ||
break; | ||
case 2: | ||
for (i = ver_right; i >= ver_left; i--) { | ||
nums[count++] = matrix[hor_bottom][i]; | ||
} | ||
hor_bottom--; | ||
break; | ||
case 3: | ||
for (i = hor_bottom; i >= hor_top; i--) { | ||
nums[count++] = matrix[i][ver_left]; | ||
} | ||
ver_left++; | ||
break; | ||
default: | ||
break; | ||
} | ||
direction++; | ||
direction %= 4; | ||
} | ||
|
||
return nums; | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
int i, j, count = 0; | ||
int row = 3; | ||
int col = 3; | ||
int **mat = malloc(row * sizeof(int *)); | ||
for (i = 0; i < row; i++) { | ||
mat[i] = malloc(col * sizeof(int)); | ||
for (j = 0; j < col; j++) { | ||
mat[i][j] = ++count; | ||
printf("%d ", mat[i][j]); | ||
} | ||
printf("\n"); | ||
} | ||
int *nums = spiralOrder(mat, row, col); | ||
for (i = 0; i < row * col; i++) { | ||
printf("%d ", nums[i]); | ||
} | ||
printf("\n"); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
all: | ||
gcc -O2 -o test spiral_matrix.c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
/** | ||
** Return an array of arrays. | ||
** Note: The returned array must be malloced, assume caller calls free(). | ||
**/ | ||
static int** generateMatrix(int n) { | ||
int i; | ||
int **matrix = malloc(n * sizeof(int *)); | ||
int *nums = malloc(n * n * sizeof(int)); | ||
for (i = 0; i < n; i++) { | ||
matrix[i] = &nums[i * n]; | ||
} | ||
|
||
int direction = 0; | ||
int hor_top = 0; | ||
int hor_bottom = n - 1; | ||
int ver_left = 0; | ||
int ver_right = n - 1; | ||
int num = 0; | ||
while (num < n * n) { | ||
switch (direction) { | ||
case 0: | ||
for (i = ver_left; i <= ver_right; i++) { | ||
matrix[hor_top][i] = ++num; | ||
} | ||
hor_top++; | ||
break; | ||
case 1: | ||
for (i = hor_top; i <= hor_bottom; i++) { | ||
matrix[i][ver_right] = ++num; | ||
} | ||
ver_right--; | ||
break; | ||
case 2: | ||
for (i = ver_right; i >= ver_left; i--) { | ||
matrix[hor_bottom][i] = ++num; | ||
} | ||
hor_bottom--; | ||
break; | ||
case 3: | ||
for (i = hor_bottom; i >= hor_top; i--) { | ||
matrix[i][ver_left] = ++num; | ||
} | ||
ver_left++; | ||
break; | ||
} | ||
direction++; | ||
direction %= 4; | ||
} | ||
|
||
return matrix; | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
if (argc != 2) { | ||
fprintf(stderr, "Usage: ./test n\n"); | ||
exit(-1); | ||
} | ||
|
||
int i, j; | ||
int n = atoi(argv[1]); | ||
int **matrix = generateMatrix(n); | ||
for (i = 0; i < n; i++) { | ||
for (j = 0; j < n; j++) { | ||
printf("%d ", matrix[i][j]); | ||
} | ||
printf("\n"); | ||
} | ||
return 0; | ||
} |