|
| 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 | +// ] |
0 commit comments