본문 바로가기
LeetCode

[LeetCode] 73. Set Matrix Zeroes

by JungSeung 2024. 3. 13.
728x90

https://leetcode.com/
Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0's.

 

you must do it in place.

 

Example 1:

출처 : https://leetcode.com/problems/set-matrix-zeroes/description/

Input: matrix = [[1,1,1], [1,0,1], [1,1,1]]
Output[[1,0,1], [0,0,0], [1,0,1]]

 

Example 2:

출처 : https://leetcode.com/problems/set-matrix-zeroes/description/

Input: matrix = [[0,1,2,0], [3,4,5,2], [1,3,1,5]]
Output:  [[0,0,0,0], [0,4,5,0], [0,3,1,0]]

 

Constraints:

  • m == matrix.length
  • n == matrix[0].length
  • 1 <= m, n <= 200
  • -2^31 <= matrix[i][j] <= 2^31 - 1

Code:

var setZeroes = function(matrix) {
    const m = matrix.length;
    const n = matrix[0].length;
    // 배열의 첫 행과 첫 열을 사용하여 해당 행 또는 열에 0이 있는지 여부를 표시합니다.
    let firstRowHasZero = false;
    let firstColHasZero = false;
    // 첫 행을 검사하여 0이 있는지 확인합니다.
    for (let j = 0; j < n; j++) {
        if (matrix[0][j] === 0) {
            firstRowHasZero = true;
            break;
        }
    }
    // 첫 열을 검사하여 0이 있는지 확인합니다.
    for (let i = 0; i < m; i++) {
        if (matrix[i][0] === 0) {
            firstColHasZero = true;
            break;
        }
    }
    // 나머지 부분을 검사하고 0이 있으면 해당 행과 열의 첫 번째 원소를 0으로 설정합니다.
    for (let i = 1; i < m; i++) {
        for (let j = 1; j < n; j++) {
            if (matrix[i][j] === 0) {
                matrix[i][0] = 0;
                matrix[0][j] = 0;
            }
        }
    }
    // 첫 행을 기반으로 0이 있는 열에 대해 열 전체를 0으로 설정합니다.
    for (let j = 1; j < n; j++) {
        if (matrix[0][j] === 0) {
            for (let i = 1; i < m; i++) {
                matrix[i][j] = 0;
            }
        }
    }
    // 첫 열을 기반으로 0이 있는 행에 대해 행 전체를 0으로 설정합니다.
    for (let i = 1; i < m; i++) {
        if (matrix[i][0] === 0) {
            for (let j = 1; j < n; j++) {
                matrix[i][j] = 0;
            }
        }
    }
    // 첫 행과 첫 열을 처리합니다.
    if (firstRowHasZero) {
        for (let j = 0; j < n; j++) {
            matrix[0][j] = 0;
        }
    }
    if (firstColHasZero) {
        for (let i = 0; i < m; i++) {
            matrix[i][0] = 0;
        }
    }
};

 

Solution Code:

var setZeroes = function(matrix) {
    // 행과 열의 개수를 가져옵니다.
    const row = matrix.length;
    const col = matrix[0].length;
    // 행과 열에 0이 있는지 여부를 기록할 배열을 생성합니다.
    const dummyRow = new Array(row).fill(-1);
    const dummyCol = new Array(col).fill(-1);
    // 0이 있는 위치를 dummyRow와 dummyCol 배열에 표시합니다.
    for (let i = 0; i < row; i++) {
        for (let j = 0; j < col; j++) {
            if (matrix[i][j] === 0) {
                dummyRow[i] = 0;
                dummyCol[j] = 0;
            }
        }
    }
    // dummyRow와 dummyCol 배열을 기반으로 행렬을 수정합니다.
    for (let i = 0; i < row; i++) {
        for (let j = 0; j < col; j++) {
            if (dummyRow[i] === 0 || dummyCol[j] === 0) {
                matrix[i][j] = 0;
            }
        }
    }
};

 

출처 : https://leetcode.com/problemset/all/

 

728x90

'LeetCode' 카테고리의 다른 글

[LeetCode] 75. Sort Colors  (0) 2024.03.20
[LeetCode] 74. Search a 2D Matrix  (1) 2024.03.15
[LeetCode] 72. Edit Distance  (0) 2024.03.01
[LeetCode] 71. Simplify Path  (0) 2024.02.27
[LeetCode] 70. Climbing Stairs  (0) 2024.02.23