Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions leetcode/54.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

#include <stdio.h>
#include <stdlib.h>

// Function to return spiral order of matrix
int* spiralOrder(int** matrix, int n, int m, int* returnSize)
{
// Allocate space for result
int* ans = (int*)malloc(n * m * sizeof(int));
*returnSize = 0; // keeps track of how many elements are stored

// Define boundaries
int top = 0, bottom = n - 1;
int left = 0, right = m - 1;
int traversedCount = 0;
int total = n * m;

// Traverse while we haven't covered all elements
while (traversedCount < total)
{
// Traverse from left to right (top row)
for (int j = left; j <= right && traversedCount < total; j++)
{
ans[(*returnSize)++] = matrix[top][j];
traversedCount++;
}
top++;

// Traverse from top to bottom (right column)
for (int i = top; i <= bottom && traversedCount < total; i++)
{
ans[(*returnSize)++] = matrix[i][right];
traversedCount++;
}
right--;

// Traverse from right to left (bottom row)
for (int j = right; j >= left && traversedCount < total; j--)
{
ans[(*returnSize)++] = matrix[bottom][j];
traversedCount++;
}
bottom--;

// Traverse from bottom to top (left column)
for (int i = bottom; i >= top && traversedCount < total; i--)
{
ans[(*returnSize)++] = matrix[i][left];
traversedCount++;
}
left++;
}

return ans;
}