面试题 01.08. 零矩阵

链接:https://leetcode-cn.com/problems/zero-matrix-lcci/
暴力循环法
执行用时:12 ms, 在所有 C++ 提交中击败了88.06% 的用户
内存消耗:11.9 MB, 在所有 C++ 提交中击败了45.29% 的用户

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
int row = matrix.size();
int col = matrix[0].size();
int index[row][col];
memset(&index[0][0],0,sizeof(index));
int i,j;
for(i=0; i<row; ++i){
for(j=0; j<col; ++j){
if(matrix[i][j] == 0){
index[i][j] = 1;
}
}
}
for(i=0; i<row; ++i){
for(j=0; j<col; ++j){
if(index[i][j]){
for(int k=0; k<col; ++k){
matrix[i][k] = 0;
}
}
}
}
for(i=0; i<row; ++i){
for(j=0; j<col; ++j){
if(index[i][j]){
for(int k=0; k<row; ++k){
matrix[k][j] = 0;
}
}
}
}
}
};

//另一种简化版
class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
int rowLength = matrix.size();
int colLength = matrix[0].size();
vector<int> row;
vector<int> col;
int i,j;
for(i=0; i<rowLength; ++i){
for(j=0; j<colLength; ++j){
if(matrix[i][j] == 0){
row.push_back(i);
col.push_back(j);
}
}
}
for(i=0; i<rowLength; ++i){
for(j=0; j<colLength; ++j){
matrix[row[i]][j] = 0;
}
}
for(i=0; i<colLength; ++i){
for(j=0; j<colLength; ++j){
matrix[j][col[i]] = 0;
}
}
}
};

面试题 01.08. 零矩阵
https://pisces34.github.io/2021/06/05/leetcode/setZeroes/
发布于
2021年6月5日
许可协议