From 9d0024b92645418d41607284cb4c9293f8c07f0f Mon Sep 17 00:00:00 2001 From: anujgaud <146576725+anujgaud@users.noreply.github.com> Date: Sat, 16 Mar 2024 23:49:41 +0530 Subject: [PATCH] Apply formatting --- .../java/com/baeldung/matrixtozero/SetMatrixToZero.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/core-java-modules/core-java-arrays-operations-advanced-2/src/main/java/com/baeldung/matrixtozero/SetMatrixToZero.java b/core-java-modules/core-java-arrays-operations-advanced-2/src/main/java/com/baeldung/matrixtozero/SetMatrixToZero.java index 46d24b0d38..6362f3672c 100644 --- a/core-java-modules/core-java-arrays-operations-advanced-2/src/main/java/com/baeldung/matrixtozero/SetMatrixToZero.java +++ b/core-java-modules/core-java-arrays-operations-advanced-2/src/main/java/com/baeldung/matrixtozero/SetMatrixToZero.java @@ -33,6 +33,7 @@ public class SetMatrixToZero{ } } } + static void setZeroesExtraSpace(int[][] matrix) { int rows = matrix.length; int cols = matrix[0].length; @@ -48,17 +49,20 @@ public class SetMatrixToZero{ } } } + for(int row: rowSet){ for(int j = 0; j < cols; j++){ matrix[row][j] = 0; } } + for(int col: colSet) { for(int i = 0; i < rows; i++){ matrix[i][col] = 0; } } } + static void setZeroesOptimized(int[][] matrix){ int rows = matrix.length; int cols = matrix[0].length; @@ -79,6 +83,7 @@ public class SetMatrixToZero{ break; } } + for(int i = 1; i < rows; i++){ for(int j = 1; j < cols; j++){ if (matrix[i][j] == 0){ @@ -87,6 +92,7 @@ public class SetMatrixToZero{ } } } + for(int i = 1; i < rows; i++){ if(matrix[i][0] == 0){ for(int j = 1; j < cols; j++){ @@ -94,6 +100,7 @@ public class SetMatrixToZero{ } } } + for(int j = 1; j < cols; j++){ if(matrix[0][j] == 0) { for(int i = 1; i < rows; i++){ @@ -101,11 +108,13 @@ public class SetMatrixToZero{ } } } + if(firstRowZero){ for(int j = 0; j < cols; j++){ matrix[0][j] = 0; } } + if(firstColZero){ for(int i = 0; i < rows; i++){ matrix[i][0] = 0;