From e30b45e71890f32c7f4699350d104b1283665dd9 Mon Sep 17 00:00:00 2001 From: donghyeon95 Date: Sat, 25 Jan 2025 20:25:36 +0900 Subject: [PATCH] feat: Set Matrix Zeroes #283 --- set-matrix-zeroes/donghyeon95.java | 58 ++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 set-matrix-zeroes/donghyeon95.java diff --git a/set-matrix-zeroes/donghyeon95.java b/set-matrix-zeroes/donghyeon95.java new file mode 100644 index 000000000..74bf5a745 --- /dev/null +++ b/set-matrix-zeroes/donghyeon95.java @@ -0,0 +1,58 @@ +class Solution { + public void setZeroes(int[][] matrix) { + int rows = matrix.length; + int cols = matrix[0].length; + + boolean firstRowHasZero = false; + boolean firstColHasZero = false; + + // 1. 첫 번째 행과 열에 0이 있는지 확인 + for (int i = 0; i < rows; i++) { + if (matrix[i][0] == 0) { + firstColHasZero = true; + break; + } + } + for (int j = 0; j < cols; j++) { + if (matrix[0][j] == 0) { + firstRowHasZero = true; + break; + } + } + + // 2. 나머지 행렬에서 0 찾기 (첫 번째 행과 열에 기록) + for (int i = 1; i < rows; i++) { + for (int j = 1; j < cols; j++) { + if (matrix[i][j] == 0) { + matrix[i][0] = 0; // 해당 행 표시 + matrix[0][j] = 0; // 해당 열 표시 + } + } + } + + // 3. 첫 번째 행과 열의 정보를 기반으로 행렬 수정 + for (int i = 1; i < rows; i++) { + for (int j = 1; j < cols; j++) { + if (matrix[i][0] == 0 || matrix[0][j] == 0) { + matrix[i][j] = 0; + } + } + } + + // 4. 첫 번째 열 복구 + if (firstColHasZero) { + for (int i = 0; i < rows; i++) { + matrix[i][0] = 0; + } + } + + // 5. 첫 번째 행 복구 + if (firstRowHasZero) { + for (int j = 0; j < cols; j++) { + matrix[0][j] = 0; + } + } + } +} + +