From a3aca778ea88bc5ef7a6c885076e5e2baae54fa1 Mon Sep 17 00:00:00 2001 From: zaidiyazdan <117353242+zaidiyazdan@users.noreply.github.com> Date: Fri, 29 Sep 2023 23:16:33 +0530 Subject: [PATCH 1/7] Fix issue #4429: added java code for Spiral Matrix II --- .../thealgorithms/others/SpiralMatrixII.java | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/main/java/com/thealgorithms/others/SpiralMatrixII.java diff --git a/src/main/java/com/thealgorithms/others/SpiralMatrixII.java b/src/main/java/com/thealgorithms/others/SpiralMatrixII.java new file mode 100644 index 000000000000..a1c5ac54c06b --- /dev/null +++ b/src/main/java/com/thealgorithms/others/SpiralMatrixII.java @@ -0,0 +1,56 @@ +/* +Problem statement: +Given a positive integer n, generate an n x n matrix filled with elements from 1 to n2 in spiral order +Examples: +Input: n = 3 +Output: [[1,2,3],[8,9,4],[7,6,5]] +*/ +package com.thealgorithms.others; + +import java.util.Arrays; + +public class SpiralMatrixII { + public int[][] generateMatrix(int n) { + 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])); + } + } +} \ No newline at end of file From 1c8d34d19228ac3abd9cd63921aa8f95b2b90f17 Mon Sep 17 00:00:00 2001 From: zaidiyazdan <117353242+zaidiyazdan@users.noreply.github.com> Date: Fri, 29 Sep 2023 23:26:49 +0530 Subject: [PATCH 2/7] changes made according to format linter --- src/main/java/com/thealgorithms/others/SpiralMatrixII.java | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/main/java/com/thealgorithms/others/SpiralMatrixII.java b/src/main/java/com/thealgorithms/others/SpiralMatrixII.java index a1c5ac54c06b..4863a2aa5b1b 100644 --- a/src/main/java/com/thealgorithms/others/SpiralMatrixII.java +++ b/src/main/java/com/thealgorithms/others/SpiralMatrixII.java @@ -1,10 +1,3 @@ -/* -Problem statement: -Given a positive integer n, generate an n x n matrix filled with elements from 1 to n2 in spiral order -Examples: -Input: n = 3 -Output: [[1,2,3],[8,9,4],[7,6,5]] -*/ package com.thealgorithms.others; import java.util.Arrays; From 8783b2b33f7d2c98021d69402393f6f92841cc30 Mon Sep 17 00:00:00 2001 From: zaidiyazdan <117353242+zaidiyazdan@users.noreply.github.com> Date: Sat, 30 Sep 2023 00:00:53 +0530 Subject: [PATCH 3/7] Changed format --- .../thealgorithms/others/SpiralMatrixII.java | 83 ++++++++++--------- 1 file changed, 43 insertions(+), 40 deletions(-) diff --git a/src/main/java/com/thealgorithms/others/SpiralMatrixII.java b/src/main/java/com/thealgorithms/others/SpiralMatrixII.java index 4863a2aa5b1b..984071a92034 100644 --- a/src/main/java/com/thealgorithms/others/SpiralMatrixII.java +++ b/src/main/java/com/thealgorithms/others/SpiralMatrixII.java @@ -2,48 +2,51 @@ import java.util.Arrays; -public class SpiralMatrixII { - public int[][] generateMatrix(int n) { - 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 +class SpiralMatrixII { +public + int[][] generateMatrix(int n) { + 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++; } - public static void main(String[] args) { - SpiralMatrixII solution = new SpiralMatrixII(); - int n = 3; - int[][] result = solution.generateMatrix(n); + return result; + } - for (int i = 0; i < n; i++) { - System.out.println(Arrays.toString(result[i])); - } +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])); } + } } \ No newline at end of file From 8c96ae1cfdf90b654fb30254ec8a7153dd5bfe37 Mon Sep 17 00:00:00 2001 From: vil02 Date: Fri, 29 Sep 2023 21:38:20 +0200 Subject: [PATCH 4/7] style: use proper formatting --- .../thealgorithms/others/SpiralMatrixII.java | 85 +++++++++---------- 1 file changed, 41 insertions(+), 44 deletions(-) diff --git a/src/main/java/com/thealgorithms/others/SpiralMatrixII.java b/src/main/java/com/thealgorithms/others/SpiralMatrixII.java index 984071a92034..0c747ecbe098 100644 --- a/src/main/java/com/thealgorithms/others/SpiralMatrixII.java +++ b/src/main/java/com/thealgorithms/others/SpiralMatrixII.java @@ -2,51 +2,48 @@ import java.util.Arrays; -public -class SpiralMatrixII { -public - int[][] generateMatrix(int n) { - 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++; +public class SpiralMatrixII { + public int[][] generateMatrix(int n) { + 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; } - return result; - } + public static void main(String[] args) { + SpiralMatrixII solution = new SpiralMatrixII(); + int n = 3; + int[][] result = solution.generateMatrix(n); -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])); + for (int i = 0; i < n; i++) { + System.out.println(Arrays.toString(result[i])); + } } - } -} \ No newline at end of file +} From c3a183efb749fbc967c68d50f8ca689f8e0c039e Mon Sep 17 00:00:00 2001 From: zaidiyazdan <117353242+zaidiyazdan@users.noreply.github.com> Date: Sat, 30 Sep 2023 01:59:34 +0530 Subject: [PATCH 5/7] Implemented suggested changes --- .../thealgorithms/others/SpiralMatrixII.java | 88 +++++++++---------- .../others/SpiralMatrixIITest.java | 32 +++++++ 2 files changed, 73 insertions(+), 47 deletions(-) create mode 100644 src/test/java/com/thealgorithms/others/SpiralMatrixIITest.java diff --git a/src/main/java/com/thealgorithms/others/SpiralMatrixII.java b/src/main/java/com/thealgorithms/others/SpiralMatrixII.java index 984071a92034..424892ff502c 100644 --- a/src/main/java/com/thealgorithms/others/SpiralMatrixII.java +++ b/src/main/java/com/thealgorithms/others/SpiralMatrixII.java @@ -1,52 +1,46 @@ package com.thealgorithms.others; -import java.util.Arrays; - -public -class SpiralMatrixII { -public - int[][] generateMatrix(int n) { - 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++; +public final class SpiralMatrixII { + private SpiralMatrixII() { } - - 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])); + static public int[][] generateMatrix(int size) { + int[][] result = new int[size][size]; + int num = 1; + int top = 0, bottom = size - 1, left = 0, right = size - 1; + + 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++; + } + return result; + } + 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])); + } } - } } \ No newline at end of file diff --git a/src/test/java/com/thealgorithms/others/SpiralMatrixIITest.java b/src/test/java/com/thealgorithms/others/SpiralMatrixIITest.java new file mode 100644 index 000000000000..a0b676993d2c --- /dev/null +++ b/src/test/java/com/thealgorithms/others/SpiralMatrixIITest.java @@ -0,0 +1,32 @@ +package com.thealgorithms.others; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +public class SpiralMatrixIITest { + + @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); + } + } + + @Test + public void testGeneratedMatrixCorrectness() { + int[][] expectedMatrix = { + {1, 2, 3}, + {8, 9, 4}, + {7, 6, 5} + }; + + int size = 3; + int[][] result = SpiralMatrixII.generateMatrix(size); + + assertArrayEquals(expectedMatrix, result); + } +} From c7dc9c035913ee271ab763dab66809eb426cc663 Mon Sep 17 00:00:00 2001 From: zaidiyazdan <117353242+zaidiyazdan@users.noreply.github.com> Date: Sat, 30 Sep 2023 02:15:52 +0530 Subject: [PATCH 6/7] formatting done with changes --- .../java/com/thealgorithms/others/SpiralMatrixII.java | 4 ++-- .../java/com/thealgorithms/others/SpiralMatrixIITest.java | 8 ++------ 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/thealgorithms/others/SpiralMatrixII.java b/src/main/java/com/thealgorithms/others/SpiralMatrixII.java index 424892ff502c..ab0c8c24d584 100644 --- a/src/main/java/com/thealgorithms/others/SpiralMatrixII.java +++ b/src/main/java/com/thealgorithms/others/SpiralMatrixII.java @@ -3,7 +3,7 @@ public final class SpiralMatrixII { private SpiralMatrixII() { } - static public int[][] generateMatrix(int size) { + static public int[][] generateMatrix(int size) { int[][] result = new int[size][size]; int num = 1; int top = 0, bottom = size - 1, left = 0, right = size - 1; @@ -43,4 +43,4 @@ public static void main(String[] args) { System.out.println(Arrays.toString(result[i])); } } -} \ No newline at end of file +} diff --git a/src/test/java/com/thealgorithms/others/SpiralMatrixIITest.java b/src/test/java/com/thealgorithms/others/SpiralMatrixIITest.java index a0b676993d2c..f61f344ba3fa 100644 --- a/src/test/java/com/thealgorithms/others/SpiralMatrixIITest.java +++ b/src/test/java/com/thealgorithms/others/SpiralMatrixIITest.java @@ -8,7 +8,7 @@ public class SpiralMatrixIITest { @Test public void testGeneratedMatrixSize() { - int size = 3; // Change this to test different matrix sizes + 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++) { @@ -18,11 +18,7 @@ public void testGeneratedMatrixSize() { @Test public void testGeneratedMatrixCorrectness() { - int[][] expectedMatrix = { - {1, 2, 3}, - {8, 9, 4}, - {7, 6, 5} - }; + int[][] expectedMatrix = {{1, 2, 3}, {8, 9, 4}, {7, 6, 5}}; int size = 3; int[][] result = SpiralMatrixII.generateMatrix(size); From f874a140bba67190fc5a5c61094cf946d7daea62 Mon Sep 17 00:00:00 2001 From: zaidiyazdan <117353242+zaidiyazdan@users.noreply.github.com> Date: Sat, 30 Sep 2023 02:21:03 +0530 Subject: [PATCH 7/7] importing Arrays is essentical added it --- src/main/java/com/thealgorithms/others/SpiralMatrixII.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/com/thealgorithms/others/SpiralMatrixII.java b/src/main/java/com/thealgorithms/others/SpiralMatrixII.java index ab0c8c24d584..68bdf992331a 100644 --- a/src/main/java/com/thealgorithms/others/SpiralMatrixII.java +++ b/src/main/java/com/thealgorithms/others/SpiralMatrixII.java @@ -1,4 +1,5 @@ package com.thealgorithms.others; +import java.util.Arrays; public final class SpiralMatrixII { private SpiralMatrixII() {