-
Notifications
You must be signed in to change notification settings - Fork 19.9k
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
Fix issue #4429: added java code for Spiral Matrix II #4438
Conversation
zaidiyazdan
commented
Sep 29, 2023
- I have read CONTRIBUTING.md.
- This pull request is all my own work -- I have not plagiarized it.
- All filenames are in PascalCase.
- All functions and variable names follow Java naming conventions.
- All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
Can some one please help me with Clang format ? |
You can format your code with the command (run it somewhere inside this project directory): clang-format -i --style=file path/to/your/file.java If you do not have the correct version of Line 86 in ea0eef1
and add the new line symbol at the end of your file manually (but do not commit the changed .clang-format file).
If you use gitpod, everything should run smoothly there. |
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 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:
Factorial
andFactorialTest
,FindMax
andFindMaxTest
.
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.
okay i will change it
import java.util.Arrays; | ||
|
||
public class SpiralMatrixII { | ||
public int[][] generateMatrix(int n) { |
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.
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?
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.
okay i will look after it
|
||
import java.util.Arrays; | ||
|
||
public class SpiralMatrixII { |
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.
Why not to make a proper utility class out of this?
public class SpiralMatrixII { | |
public final class SpiralMatrixII { | |
private SpiralMatrixII() { | |
} |
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.
Will do it
@@ -0,0 +1,49 @@ | |||
package com.thealgorithms.others; | |||
|
|||
import java.util.Arrays; |
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?
import java.util.Arrays; |
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
@vil02 done with all the changes please check it out |
public static void main(String[] args) { | ||
SpiralMatrixII solution = new SpiralMatrixII(); | ||
int size = 3; | ||
int[][] result = solution.generateMatrix(size); | ||
for (int i = 0; i < size; i++) { | ||
System.out.println(Arrays.toString(result[i])); | ||
} | ||
} |
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.
public static void main(String[] args) { | |
SpiralMatrixII solution = new SpiralMatrixII(); | |
int size = 3; | |
int[][] result = solution.generateMatrix(size); | |
for (int i = 0; i < size; i++) { | |
System.out.println(Arrays.toString(result[i])); | |
} | |
} |
public final class SpiralMatrixII { | ||
private SpiralMatrixII() { | ||
} | ||
static public int[][] generateMatrix(int size) { |
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.
static public int[][] generateMatrix(int size) { | |
public static int[][] generateMatrix(final int size) { |
while (num <= size * size) { | ||
// Traverse right | ||
for (int i = left; i <= right && num <= size * size; i++) { | ||
result[top][i] = num++; | ||
} | ||
top++; | ||
|
||
// Traverse down | ||
for (int i = top; i <= bottom && num <= size * size; i++) { | ||
result[i][right] = num++; | ||
} | ||
right--; | ||
|
||
// Traverse left | ||
for (int i = right; i >= left && num <= size * size; i--) { | ||
result[bottom][i] = num++; | ||
} | ||
bottom--; | ||
|
||
// Traverse up | ||
for (int i = bottom; i >= top && num <= size * size; i--) { | ||
result[i][left] = num++; | ||
} | ||
left++; | ||
} |
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 should be simplified.
@Test | ||
public void testGeneratedMatrixSize() { | ||
int size = 3; // Change this to test different matrix sizes | ||
int[][] result = SpiralMatrixII.generateMatrix(size); | ||
assertEquals(size, result.length); | ||
for (int i = 0; i < size; i++) { | ||
assertEquals(size, result[i].length); | ||
} | ||
} |
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 test does not add any value: if testGeneratedMatrixCorrectness
passes, this will also pass.
@Test | |
public void testGeneratedMatrixSize() { | |
int size = 3; // Change this to test different matrix sizes | |
int[][] result = SpiralMatrixII.generateMatrix(size); | |
assertEquals(size, result.length); | |
for (int i = 0; i < size; i++) { | |
assertEquals(size, result[i].length); | |
} | |
} |
@zaidiyazdan are you still working on this? if not, please close this PR. |