Skip to content

Commit 738ca31

Browse files
committed
✨ [73] Set Matrix Zeroes
Runtime 99 ms Beats 11.28% Memory 45.5 MB Beats 21.77%
1 parent ddc237d commit 738ca31

File tree

3 files changed

+133
-1
lines changed

3 files changed

+133
-1
lines changed

73/my_solution.js

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/**
2+
* @param {number[][]} matrix
3+
* @return {void} Do not return anything, modify matrix in-place instead.
4+
*/
5+
const setZeroes = (matrix) => {
6+
console.log(`rowL:${matrix.length}, colL:${matrix[0].length}`)
7+
console.log(matrix)
8+
9+
const expand = (row, col) => {
10+
// if (row < 0 || col < 0 || row === matrix.length || col === matrix[0].length) {
11+
// console.log(`--> row:${row}, col:${col} - EXIT`)
12+
// return;
13+
// }
14+
15+
let directions = [[0, 1], [1, 0], [0, -1], [-1, 0]];
16+
17+
console.log(matrix)
18+
19+
for (let [r, c] of directions) {
20+
let factor = 1;
21+
// while (undefined !== matrix[row + (r * factor)][col + (c * factor)]) {
22+
// let newR = row + (r * factor);
23+
// let newC = col + (c * factor);
24+
while (
25+
// (newR >= 0) &&
26+
// (newC >= 0) &&
27+
// (newR < matrix.length) &&
28+
// (newC < matrix[0].length)
29+
((row + (r * factor)) >= 0) &&
30+
((col + (c * factor)) >= 0) &&
31+
((row + (r * factor)) < matrix.length) &&
32+
((col + (c * factor)) < matrix[0].length)
33+
) {
34+
// console.log(`newR:${newR}, newC:${newC}, matrix[r][c]:${matrix[newR][newC]}`)
35+
// matrix[newR][newC] = 0;
36+
matrix[row + (r * factor)][col + (c * factor)] = 0;
37+
factor++;
38+
39+
// console.log(`> newR:${newR}, newC:${newC}`)
40+
}
41+
}
42+
43+
// matrix[row][col] = 0;
44+
// dfs(row, col + 1);
45+
// dfs(row + 1, col);
46+
// dfs(row, col - 1);
47+
// dfs(row - 1, col);
48+
}
49+
50+
coordinate = [];
51+
for (let row = 0; row < matrix.length; row++) {
52+
for (let col = 0; col < matrix[row].length; col++) {
53+
if (matrix[row][col] === 0) {
54+
coordinate.push([row, col])
55+
console.log(`> row:${row}, col:${col}`)
56+
}
57+
}
58+
}
59+
60+
console.log("coordinate")
61+
console.log(coordinate)
62+
63+
for (let [row, col] of coordinate) {
64+
console.log(`-> row:${row}, col:${col}`)
65+
expand(row, col)
66+
}
67+
68+
console.log("matrix")
69+
console.log(matrix)
70+
71+
};
72+
73+
// setZeroes([
74+
// [1, 1, 1],
75+
// [1, 0, 1],
76+
// [1, 1, 1]
77+
// ]);
78+
79+
// [
80+
// [1, 0, 1],
81+
// [0, 0, 0],
82+
// [1, 0, 1]
83+
// ]
84+
85+
86+
setZeroes([
87+
[0, 1, 2, 0],
88+
[3, 4, 5, 2],
89+
[1, 3, 1, 5]
90+
]);
91+
92+
// [
93+
// [0, 0, 0, 0],
94+
// [0, 4, 5, 0],
95+
// [0, 3, 1, 0]
96+
// ]

73/solution.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @param {number[][]} matrix
3+
* @return {void} Do not return anything, modify matrix in-place instead.
4+
*/
5+
const setZeroes = (matrix) => {
6+
const directions = [[0, 1], [1, 0], [0, -1], [-1, 0]];
7+
8+
const expand = (row, col) => {
9+
for (let [r, c] of directions) {
10+
let factor = 1;
11+
while (
12+
((row + (r * factor)) >= 0) &&
13+
((col + (c * factor)) >= 0) &&
14+
((row + (r * factor)) < matrix.length) &&
15+
((col + (c * factor)) < matrix[0].length)
16+
) {
17+
matrix[row + (r * factor)][col + (c * factor)] = 0;
18+
factor++;
19+
}
20+
}
21+
}
22+
23+
coordinate = [];
24+
for (let row = 0; row < matrix.length; row++) {
25+
for (let col = 0; col < matrix[row].length; col++) {
26+
if (matrix[row][col] === 0) {
27+
coordinate.push([row, col])
28+
}
29+
}
30+
}
31+
32+
for (let [row, col] of coordinate) {
33+
expand(row, col)
34+
}
35+
};

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
- [53. Maximum Subarray](./53/)
3030
- [56. Merge Intervals](./56/)
3131
- [57. Insert Interval](./57/)
32+
- [73. Set Matrix Zeroes](./73/)
3233
- [76. Minimum Window Substring](./76/)
3334
- [83. Remove Duplicates from Sorted List](./83/)
3435
- [121. Best Time to Buy and Sell Stock](./121/)
@@ -109,7 +110,7 @@ Batch create:
109110
NOTE: JS IS HERE
110111
-->
111112
```ssh
112-
chapter=128 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
113+
chapter=73 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
113114
```
114115
> then you can use `x` for quick debug.
115116

0 commit comments

Comments
 (0)