반응형
1. 문제
https://leetcode.com/problems/set-matrix-zeroes/
2.코드
/**
* @param {number[][]} matrix
* @return {void} Do not return anything, modify matrix in-place instead.
*/
var setZeroes = function(matrix) {
let queue = [];
for(let i=0;i<matrix.length;i++){
for(let j=0;j<matrix[i].length;j++){
if(!matrix[i][j]) queue.push([i,j]);
}
}
while(queue.length){
let cur = queue.shift();
for(let i=0;i<matrix[0].length;i++){
if(matrix[cur[0]][i])
matrix[cur[0]][i] = 0;
}
for(let i=0;i<matrix.length;i++){
if(matrix[i][cur[1]])
matrix[i][cur[1]] = 0;
}
}
};
반응형
'리트코드 > midium' 카테고리의 다른 글
[리트코드] 438. Find All Anagrams in a String - js (sliding window) (0) | 2023.09.08 |
---|---|
[리트코드] 34. Find First and Last Position of Element in Sorted Array - js (투포인터) (0) | 2023.09.04 |
[리트코드] 300. Longest Increasing Subsequence - js (DP) (0) | 2023.08.31 |
[리트코드] 994. Rotting Oranges - js (BFS) (0) | 2023.08.30 |
[리트코드] 11. Container With Most Water - js (투포인터) (1) | 2023.08.28 |