Skip to content

Fix issue #4429: added java code for Spiral Matrix II #4438

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

Closed
wants to merge 8 commits into from
Closed
Changes from 4 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
49 changes: 49 additions & 0 deletions src/main/java/com/thealgorithms/others/SpiralMatrixII.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.thealgorithms.others;

import java.util.Arrays;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This import is not used. Is that right?

Suggested change
import java.util.Arrays;

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ohh yup ... Sorry i would have missed it


public class SpiralMatrixII {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not to make a proper utility class out of this?

Suggested change
public class SpiralMatrixII {
public final class SpiralMatrixII {
private SpiralMatrixII() {
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do it

public int[][] generateMatrix(int n) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public int[][] generateMatrix(int n) {
static public int[][] generateMatrix(int n) {

I would l suggest to rename the variable n to something like size.

What happens if n is negative?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay i will look after it

int[][] result = new int[n][n];
int num = 1;
int top = 0, bottom = n - 1, left = 0, right = n - 1;

while (num <= n * n) {
// Traverse right
for (int i = left; i <= right && num <= n * n; i++) {
result[top][i] = num++;
}
top++;

// Traverse down
for (int i = top; i <= bottom && num <= n * n; i++) {
result[i][right] = num++;
}
right--;

// Traverse left
for (int i = right; i >= left && num <= n * n; i--) {
result[bottom][i] = num++;
}
bottom--;

// Traverse up
for (int i = bottom; i >= top && num <= n * n; i--) {
result[i][left] = num++;
}
left++;
}

return result;
}

public static void main(String[] args) {
SpiralMatrixII solution = new SpiralMatrixII();
int n = 3;
int[][] result = solution.generateMatrix(n);

for (int i = 0; i < n; i++) {
System.out.println(Arrays.toString(result[i]));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the main method and add some proper tests. Good examples to look at:

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay i will change it

}
}