Skip to content

Commit

Permalink
Spiral matrix
Browse files Browse the repository at this point in the history
Signed-off-by: begeekmyfriend <[email protected]>
  • Loading branch information
begeekmyfriend committed Aug 31, 2017
1 parent afc05f0 commit eebb0bd
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 0 deletions.
2 changes: 2 additions & 0 deletions 054_spiral_matrix/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
gcc -O2 -o test spiral_matrix.c
73 changes: 73 additions & 0 deletions 054_spiral_matrix/spiral_matrix.c
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;
}
2 changes: 2 additions & 0 deletions 059_spiral_matrix_ii/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
gcc -O2 -o test spiral_matrix.c
73 changes: 73 additions & 0 deletions 059_spiral_matrix_ii/spiral_matrix.c
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;
}

0 comments on commit eebb0bd

Please sign in to comment.