LeetCode Set Matrix Zeroes (JavaScript)

2025. 12. 16. 18:27·코딩테스트/LeetCode

링크 : https://leetcode.com/problems/set-matrix-zeroes/description/

문제 설명

더보기

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:

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

Example 2:

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
  • 231 <= matrix[i][j] <= 231 - 1

Follow up:

  • A straightforward solution using O(mn) space is probably a bad idea.
  • A simple improvement uses O(m + n) space, but still not the best solution.
  • Could you devise a constant space solution?

문제 풀이

첫 번째 반복문

  • 각 row를 돌면서 0의 index (j)를 set에 저장, 해당 row에 0이 있다면 zero를 true로 바꿈
  • zero가 true라면 해당 라인은 0으로 채워야 하기 때문에 row의 모든 col을 0으로 변환

두 번째 반복문

  • 각 row의 0 index를 저장해 두었고 해당 index의 위, 아래도 0으로 변환해야 하기 때문에 matrix를 돌면서 해당 col index가 set에 속해 있다면 0으로 변환

최종적으로 matrix 반환

/**
 * @param {number[][]} matrix
 * @return {void} Do not return anything, modify matrix in-place instead.
 */
var setZeroes = function(matrix) {
    const set = new Set();

    for (let i = 0; i < matrix.length; i++) {
        let zero = false;
        for (let j = 0; j < matrix[i].length; j++) {
            if (matrix[i][j] === 0) {
                set.add(j);
                zero = true;
            }
        }

        if (zero) {
            for (let j = 0; j < matrix[i].length; j++) {
                matrix[i][j] = 0;
            }
        }
    }

    for (let i = 0; i < matrix.length; i++) {
        for (let j = 0; j < matrix[i].length; j++) {
            if (set.has(j)) {
                matrix[i][j] = 0;
            }
        }
    }

    return matrix;
};

결론

0으로 변환하는 부분을 2개로 나누어 해결

단순 구현 문제

'코딩테스트 > LeetCode' 카테고리의 다른 글

LeetCode Search in Rotated Sorted Array (JavaScript)  (0) 2025.12.17
LeetCode Remove Duplicates from Sorted Array ll (JavaScript)  (0) 2025.12.17
LeetCode Simplify Path (JavaScript)  (1) 2025.12.15
LeetCode Minimum Path Sum (JavaScript)  (0) 2025.12.11
LeetCode Unique Paths (JavaScript)  (0) 2025.12.10
'코딩테스트/LeetCode' 카테고리의 다른 글
  • LeetCode Search in Rotated Sorted Array (JavaScript)
  • LeetCode Remove Duplicates from Sorted Array ll (JavaScript)
  • LeetCode Simplify Path (JavaScript)
  • LeetCode Minimum Path Sum (JavaScript)
의현
의현
개발하는 하루
  • 의현
    UIHYEON
    의현
  • 링크

    • 김의현 포트폴리오
    • GitHub
    • LinkedIn
  • 전체
    오늘
    어제
    • 분류 전체보기 (215) N
      • 프론트엔드 (65) N
        • JavaScript (52) N
        • HTML (3)
        • React (7)
        • CSS (2)
        • CS (1)
      • 프로젝트 (19)
        • Portfolio 사이트 개발 (19)
      • 코딩테스트 (128) N
        • Binary Search (2)
        • bfs (Breadth-first s.. (4)
        • dfs (Deapth-first se.. (1)
        • Greedy (1)
        • Dynamic Programming (1)
        • two pointer (4)
        • 구현 (2)
        • LIS(Longest Increasi.. (0)
        • 문자열 (3)
        • 자료구조 (6)
        • 비트마스크 (2)
        • 수학 (2)
        • 프로그래머스 (61)
        • LeetCode (39) N
  • 인기 글

  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
의현
LeetCode Set Matrix Zeroes (JavaScript)
상단으로

티스토리툴바