-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create SpiralOrderMatrix.java #78
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import java.util.ArrayList; | ||
|
||
class Solution { | ||
public ArrayList<ArrayList<Integer>> generateMatrix(int n) { | ||
// Define the boundaries for traversal in the 2D array | ||
int top = 0; | ||
int bottom = n - 1; | ||
int left = 0; | ||
int right = n - 1; | ||
int counter = 1; // Initialize counter to populate the array with values from 1 to n^2 | ||
|
||
// Initialize an n x n matrix to store numbers in a spiral order | ||
int[][] arr = new int[n][n]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (complexity): Consider directly building the ArrayList during spiral traversal instead of using an intermediate array The code can be simplified by directly building the ArrayList<ArrayList> during spiral traversal, eliminating the need for conversion between data structures. This reduces both memory usage and code complexity: public ArrayList<ArrayList<Integer>> generateMatrix(int n) {
ArrayList<ArrayList<Integer>> matrix = new ArrayList<>();
// Initialize the matrix with n rows of n elements
for (int i = 0; i < n; i++) {
ArrayList<Integer> row = new ArrayList<>();
for (int j = 0; j < n; j++) {
row.add(0);
}
matrix.add(row);
}
int top = 0, bottom = n - 1, left = 0, right = n - 1;
int counter = 1;
while (counter <= n * n) {
for (int i = left; i <= right; i++)
matrix.get(top).set(i, counter++);
top++;
for (int i = top; i <= bottom; i++)
matrix.get(i).set(right, counter++);
right--;
for (int i = right; i >= left; i--)
matrix.get(bottom).set(i, counter++);
bottom--;
for (int i = bottom; i >= top; i--)
matrix.get(i).set(left, counter++);
left++;
}
return matrix;
} This version:
|
||
|
||
// Loop until the counter reaches n^2 (we've filled the entire matrix) | ||
while (counter <= (n * n)) { | ||
// Traverse from left to right on the top row | ||
for (int i = left; i <= right; i++) { | ||
arr[top][i] = counter++; | ||
} | ||
top++; // Move the top boundary down | ||
|
||
// Traverse from top to bottom on the rightmost column | ||
for (int i = top; i <= bottom; i++) { | ||
arr[i][right] = counter++; | ||
} | ||
right--; // Move the right boundary left | ||
|
||
// Traverse from right to left on the bottom row (if bottom row is still valid) | ||
for (int i = right; i >= left; i--) { | ||
arr[bottom][i] = counter++; | ||
} | ||
bottom--; // Move the bottom boundary up | ||
|
||
// Traverse from bottom to top on the leftmost column (if left column is still valid) | ||
for (int i = bottom; i >= top; i--) { | ||
arr[i][left] = counter++; | ||
} | ||
left++; // Move the left boundary right | ||
} | ||
|
||
// Initialize the ArrayList to hold the 2D spiral order matrix | ||
ArrayList<ArrayList<Integer>> list = new ArrayList<>(); | ||
|
||
// Convert 2D array to an ArrayList of ArrayLists | ||
for (int i = 0; i < arr.length; i++) { | ||
ArrayList<Integer> rowList = new ArrayList<>(); // Temporary list to hold each row | ||
for (int j = 0; j < arr[0].length; j++) { | ||
rowList.add(arr[i][j]); // Add each element in the row to the list | ||
} | ||
list.add(rowList); // Add each row list to the final list | ||
} | ||
|
||
// Return the final spiral order matrix as an ArrayList of ArrayLists | ||
return list; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue: Add input validation for parameter 'n' to handle negative values
The method should throw an IllegalArgumentException when n < 0 to prevent array allocation issues