Created Spiral_Matrix code from leetcode#759
Open
02-Shivaraj wants to merge 1 commit intocodemistic:mainfrom
Open
Created Spiral_Matrix code from leetcode#75902-Shivaraj wants to merge 1 commit intocodemistic:mainfrom
02-Shivaraj wants to merge 1 commit intocodemistic:mainfrom
Conversation
SrishtiSinha2003
approved these changes
Jun 23, 2025
| int count = 0; | ||
| int row = 0; | ||
| int col = 0; | ||
| boolean right = true; |
There was a problem hiding this comment.
Too many boolean flags for directions
- right, left, up, down, and curr lead to bloated logic and repetitive conditionals.
- This could be replaced by a direction array and an index to keep it clean.
There was a problem hiding this comment.
`public List spiralOrder(int[][] matrix) {
List result = new ArrayList<>();
if (matrix == null || matrix.length == 0) return result;
int top = 0;
int bottom = matrix.length - 1;
int left = 0;
int right = matrix[0].length - 1;
while (top <= bottom && left <= right) {
// move right
for (int i = left; i <= right; i++) {
result.add(matrix[top][i]);
}
top++;
// move down
for (int i = top; i <= bottom; i++) {
result.add(matrix[i][right]);
}
right--;
// move left
if (top <= bottom) {
for (int i = right; i >= left; i--) {
result.add(matrix[bottom][i]);
}
bottom--;
}
// move up
if (left <= right) {
for (int i = bottom; i >= top; i--) {
result.add(matrix[i][left]);
}
left++;
}
}
return result;
}
}`
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.