리트코드/midium
[리트코드] 73. Set Matrix Zeroes - js (queue)
bbeyak
2023. 9. 1. 13:30
반응형
1. 문제
https://leetcode.com/problems/set-matrix-zeroes/
Set Matrix Zeroes - LeetCode
Can you solve this real interview question? Set Matrix Zeroes - 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 [https://en.wikipedia.org/wiki/In-place_algorithm]. Example 1: [https
leetcode.com
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;
}
}
};
반응형