Voyz's Studio.

LeetCode算法笔记--Day5

字数统计: 222阅读时长: 1 min
2020/09/08 Share

LeetCode算法笔记–零矩阵

01.08. 零矩阵

题目:

编写一种算法,若M × N矩阵中某个元素为0,则将其所在的行与列清零。

示例1:
输入: [
[1,1,1],
[1,0,1],
[1,1,1]
]
输出: [
[1,0,1],
[0,0,0],
[1,0,1]
]

示例2:
输入: [
[0,1,2,0],
[3,4,5,2],
[1,3,1,5]
]
输出: [
[0,0,0,0],
[0,4,5,0],
[0,3,1,0]
]

我的解答:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
* @param {number[][]} matrix
* @return {void} Do not return anything, modify matrix in-place instead.
*/
var setZeroes = function(matrix) {
let _new_matrix = JSON.parse(JSON.stringify(matrix));
let _output = []
let _row = _new_matrix.length;
let _cloumn = _new_matrix[0].length

for(let r=0;r<_row;r++){
for(let c=0;c<_cloumn;c++){
if(_new_matrix[r][c] == 0){
matrix = clearMatrix(matrix,_row,_cloumn,r,c)
}
}
}
return matrix
};

var clearMatrix = function(matrix,_row,_cloumn,_r,_c){
for(let r=0;r<_row;r++){
for(let c=0;c<_cloumn;c++){
matrix[_r][c] = 0;
matrix[r][_c] = 0;
}
}
return matrix;
}
CATALOG
  1. 1. LeetCode算法笔记–零矩阵
  2. 2. 01.08. 零矩阵
    1. 2.0.0.0.1. 题目:
    2. 2.0.0.0.2. 我的解答: