leetcode73:矩阵置零

题目链接

leetcode

题目描述

给定一个 m x n 的矩阵,如果一个元素为 0 ,则将其所在行和列的所有元素都设为 0
请使用 原地 算法。

C++ 代码

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <iostream>
#include <vector>
using namespace std;

/*
使用标记数组

时间复杂度:O(mn)
其中 m 是矩阵的行数,n 是矩阵的列数。
遍历矩阵两次。
空间复杂度:O(m+n)
分别记录每一行或每一列是否有 0。
*/
class Solution_0 {
public:
void setZeroes(vector<vector<int>>& matrix) {
if (matrix.empty() || matrix[0].empty()) {
return;
}

int m = matrix.size(), n = matrix[0].size();
vector<int> row(m), col(n);

for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (!matrix[i][j]) {
row[i] = col[j] = 1;
}
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (row[i] || col[j]) {
matrix[i][j] = 0;
}
}
}
}
};

/*
使用两个标记变量
使用其他行与列去处理第一行与第一列,
使用第一行与第一列去更新其他行与列,
最后使用两个标记变量更新第一行与第一列。

时间复杂度:O(mn)
其中 m 是矩阵的行数,n 是矩阵的列数。
遍历该矩阵两次。
空间复杂度:O(1)
使用两个变量,记录第一行和第一列是否有 0。
*/
class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
if (matrix.empty() || matrix[0].empty()) {
return;
}

int m = matrix.size(), n = matrix[0].size();
int row = 1, col = 1; // 初始化标记变量

// 检查第一列是否有 0
for (int i = 0; i < m; i++) {
if (!matrix[i][0]) {
col = 0;
}
}
// 检查第一行是否有 0
for (int i = 0; i < n; i++) {
if (!matrix[0][i]) {
row = 0;
}
}

// 使用第一行/第一列作为标记数组
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
if (!matrix[i][j]) {
matrix[i][0] = matrix[0][j] = 0;
}
}
}
// 根据标记,将其他位置设置为 0
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
if (!matrix[i][0] || !matrix[0][j]) {
matrix[i][j] = 0;
}
}
}

// 最后处理第一行和第一列
if (!col) {
for (int i = 0; i < m; i++) {
matrix[i][0] = 0;
}
}
if (!row) {
for (int i = 0; i < n; i++) {
matrix[0][i] = 0;
}
}
}
};

// 辅助函数:打印数组
void printArray(const vector<int>& nums) {
cout << "[";
for (size_t i = 0; i < nums.size(); i++) {
cout << nums[i];
if (i != nums.size() - 1) cout << ",";
}
cout << "]";
}

// 辅助函数:打印二维数组
void printArray(const vector<vector<int>>& nums) {
cout << "[";
for (size_t i = 0; i < nums.size(); i++) {
cout << "[";
for (size_t j = 0; j < nums[i].size(); j++) {
cout << nums[i][j];
if (j != nums[i].size() - 1) cout << ",";
}
cout << "]";
if (i != nums.size() - 1) cout << ",";
}
cout << "]";
}

int main() {
Solution solution;
vector<vector<vector<int>>> matrix_cases = {
{{{1,1,1},{1,0,1},{1,1,1}}},
{{{0,1,2,0},{3,4,5,2},{1,3,1,5}}},
{{{0}}}
};

for (size_t i = 0; i < matrix_cases.size(); i++) {
auto& matrix = matrix_cases[i];

cout << "Input: ";
printArray(matrix);
cout << endl;
solution.setZeroes(matrix);
cout << "Output: ";
printArray(matrix);
cout << endl;
}

return 0;
}

leetcode73:矩阵置零
https://lcf163.github.io/2023/11/18/leetcode73:矩阵置零/
作者
乘风的小站
发布于
2023年11月18日
许可协议