-
Notifications
You must be signed in to change notification settings - Fork 20k
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
Changes from 4 commits
a3aca77
1c8d34d
8783b2b
8c96ae1
c3a183e
bda1216
c7dc9c0
f874a14
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,49 @@ | ||||||||||
package com.thealgorithms.others; | ||||||||||
|
||||||||||
import java.util.Arrays; | ||||||||||
|
||||||||||
public class SpiralMatrixII { | ||||||||||
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. Why not to make a proper utility class out of this?
Suggested change
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. Will do it |
||||||||||
public int[][] generateMatrix(int 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.
Suggested change
I would l suggest to rename the variable What happens if 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. 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])); | ||||||||||
} | ||||||||||
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. Please remove the
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. okay i will change it |
||||||||||
} | ||||||||||
} |
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.
This import is not used. Is that right?
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.
ohh yup ... Sorry i would have missed it