mirror of
https://github.com/TheAlgorithms/C.git
synced 2026-02-04 05:44:35 +00:00
[PR #1493] [CLOSED] Solved the question of spiral matrix traversal (leetcode) #2093
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/TheAlgorithms/C/pull/1493
Author: @Ujjansh05
Created: 10/3/2025
Status: ❌ Closed
Base:
master← Head:master📝 Commits (1)
993f5ffSolved the question of spiral matrix traversal (leetcode)📊 Changes
1 file changed (+55 additions, -0 deletions)
View changed files
➕
leetcode/54.c(+55 -0)📄 Description
Implemented a solution for the "Spiral Matrix" problem (LeetCode #54). The new file 54.C contains the function:
which computes the elements of an n x m matrix in spiral order and returns a dynamically allocated array containing the traversal. The function updates *returnSize with the number of returned elements.
Key details:
Approach: Boundary-tracking traversal (top, bottom, left, right) that iteratively collects rows/columns while shrinking boundaries.
Time complexity: O(n * m) — each matrix element is visited exactly once.
Space complexity: O(n * m) for the returned array (output storage). No extra traversal buffers used.
Assumptions / Notes:
The function expects valid pointers for matrix and returnSize, and positive n and m.
The returned array is allocated with malloc; the caller is responsible for freeing it.
The implementation uses a traversal counter to avoid overrun when the matrix dimensions are uneven.
Suggested follow-ups (not required for this PR):
Add a small unit test / example harness (stdin-driven or fixed test cases) under leetcode/tests/ or repository test pattern.
Validate/handle the edge cases explicitly (e.g., n == 0 or m == 0, or NULL matrix) if the project style requires defensive checks.
Consider renaming the file to use a lowercase extension (54.c) if that matches the repository filename conventions.
References
Problem: LeetCode — Spiral Matrix: https://leetcode.com/problems/spiral-matrix/
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.