Skip to content

Commit 726340d

Browse files
committed
Add solution #73
1 parent 8e11b11 commit 726340d

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
58|[Length of Last Word](./0058-length-of-last-word.js)|Easy|
3434
66|[Plus One](./0066-plus-one.js)|Easy|
3535
67|[Add Binary](./0067-add-binary.js)|Easy|
36+
73|[Set Matrix Zeroes](./0073-set-matrix-zeroes.js)|Medium|
3637
88|[Merge Sorted Array](./0088-merge-sorted-array.js)|Easy|
3738
151|[Reverse Words in a String](./0151-reverse-words-in-a-string.js)|Medium|
3839
152|[Maximum Product Subarray](./0152-maximum-product-subarray.js)|Medium|

solutions/0073-set-matrix-zeroes.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* 73. Set Matrix Zeroes
3+
* https://leetcode.com/problems/set-matrix-zeroes/
4+
* Difficulty: Medium
5+
*
6+
* Given an `m x n` integer `matrix` matrix, if an element is `0`, set its entire
7+
* row and column to `0`'s, and return the matrix.
8+
*
9+
* You must do it in place.
10+
*/
11+
12+
/**
13+
* @param {number[][]} matrix
14+
* @return {void} Do not return anything, modify matrix in-place instead.
15+
*/
16+
var setZeroes = function(matrix) {
17+
const columns = new Set();
18+
const rows = new Set();
19+
20+
matrix.forEach((row, i) => {
21+
row.forEach((value, j) => {
22+
if (value === 0) {
23+
columns.add(i);
24+
rows.add(j);
25+
}
26+
});
27+
});
28+
29+
[...columns].forEach(i => matrix[i].forEach((_, j) => matrix[i][j] = 0));
30+
matrix.forEach((_, i) => [...rows].forEach(j => matrix[i][j] = 0));
31+
32+
return matrix;
33+
};

0 commit comments

Comments
 (0)