LeetCode: Set Matrix Zeroes Solution

1/**
2 * @param {number[][]} matrix
3 * @return {void} Do not return anything, modify matrix in-place instead.
4 */
5var setZeroes = function (matrix) {
6 const N = matrix.length
7 const M = matrix[0].length
8 let setI = new Set()
9 let setJ = new Set()
10
11 for (let i = 0; i < N; i++) {
12 for (let j = 0; j < M; j++) {
13 if (matrix[i][j] === 0) {
14 setI.add(i)
15 setJ.add(j)
16 }
17 }
18 }
19
20 for (let i = 0; i < N; i++) {
21 for (let j = 0; j < M; j++) {
22 if (setI.has(i) || setJ.has(j)) {
23 matrix[i][j] = 0
24 }
25 }
26 }
27
28 return matrix
29}

Comments

Loading comments...

Tags

leetcode

array

Apply and earn a $2,500 bonus once you're hired on your first job!

Clients from the Fortune 500 to Silicon Valley startups

Choose your own rate, get paid on time

From hourly, part-time, to full-time positions

Flexible remote working environment

A lot of open JavaScript jobs!!

Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.

Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.

Next Post

LeetCode: Group Anagrams

HoningJS

Search Posts