Skip to content

Commit 57010f9

Browse files
committed
feat: add solutions to lcci problem: No.01.08
No.01.08.Zero Matrix
1 parent 7badd5e commit 57010f9

File tree

7 files changed

+650
-141
lines changed

7 files changed

+650
-141
lines changed

lcci/01.08.Zero Matrix/README.md

+293-48
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,17 @@
4545

4646
<!-- 这里可写通用的实现逻辑 -->
4747

48-
用 set 记录需要清零的行 `zero_rows` 跟列 `zero_cols`,之后分别将需要清零的行、列上的所有元素清零。
48+
**方法一:数组标记**
49+
50+
我们分别用数组 `rows``cols` 记录待清零的行和列,最后再遍历一遍矩阵,将 `rows``cols` 中记录的行和列对应的元素清零。
51+
52+
时间复杂度 $O(m\times n)$,空间复杂度 $O(m+n)$。其中 $m$ 和 $n$ 分别为矩阵的行数和列数。
53+
54+
**方法二:原地标记**
55+
56+
我们可以用矩阵的第一行和第一列来记录待清零的行和列,但是这样会导致第一行和第一列本身也需要被清零,因此我们需要额外的变量 $i0$, $j0$ 来记录第一行、第一列是否需要被清零。
57+
58+
时间复杂度 $O(m\times n)$,空间复杂度 $O(1)$。其中 $m$ 和 $n$ 分别为矩阵的行数和列数。
4959

5060
<!-- tabs:start -->
5161

@@ -56,28 +66,39 @@
5666
```python
5767
class Solution:
5868
def setZeroes(self, matrix: List[List[int]]) -> None:
59-
"""
60-
Do not return anything, modify matrix in-place instead.
61-
"""
62-
rows, cols = len(matrix), len(matrix[0])
63-
zero_rows, zero_cols = set(), set()
64-
for i in range(rows):
65-
for j in range(cols):
66-
if matrix[i][j] == 0:
67-
zero_rows.add(i)
68-
zero_cols.add(j)
69-
70-
# 行清零
71-
for i in zero_rows:
72-
for j in range(cols):
73-
matrix[i][j] = 0
74-
75-
# 列清零
76-
for j in zero_cols:
77-
for i in range(rows):
78-
matrix[i][j] = 0
69+
m, n = len(matrix), len(matrix[0])
70+
rows = [0] * m
71+
cols = [0] * n
72+
for i, row in enumerate(matrix):
73+
for j, v in enumerate(row):
74+
if v == 0:
75+
rows[i] = cols[j] = 1
76+
for i in range(m):
77+
for j in range(n):
78+
if rows[i] or cols[j]:
79+
matrix[i][j] = 0
80+
```
7981

80-
return matrix
82+
```python
83+
class Solution:
84+
def setZeroes(self, matrix: List[List[int]]) -> None:
85+
m, n = len(matrix), len(matrix[0])
86+
i0 = any(v == 0 for v in matrix[0])
87+
j0 = any(matrix[i][0] == 0 for i in range(m))
88+
for i in range(1, m):
89+
for j in range(1, n):
90+
if matrix[i][j] == 0:
91+
matrix[i][0] = matrix[0][j] = 0
92+
for i in range(1, m):
93+
for j in range(1, n):
94+
if matrix[i][0] == 0 or matrix[0][j] == 0:
95+
matrix[i][j] = 0
96+
if i0:
97+
for j in range(n):
98+
matrix[0][j] = 0
99+
if j0:
100+
for i in range(m):
101+
matrix[i][0] = 0
81102
```
82103

83104
### **Java**
@@ -87,32 +108,214 @@ class Solution:
87108
```java
88109
class Solution {
89110
public void setZeroes(int[][] matrix) {
90-
int rows = matrix.length, cols = matrix[0].length;
91-
Set<Integer> zeroRows = new HashSet<>();
92-
Set<Integer> zeroCols = new HashSet<>();
93-
for (int i = 0; i < rows; ++i) {
94-
for (int j = 0; j < cols; ++j) {
111+
int m = matrix.length, n = matrix[0].length;
112+
boolean[] rows = new boolean[m];
113+
boolean[] cols = new boolean[n];
114+
for (int i = 0; i < m; ++i) {
115+
for (int j = 0; j < n; ++j) {
95116
if (matrix[i][j] == 0) {
96-
zeroRows.add(i);
97-
zeroCols.add(j);
117+
rows[i] = true;
118+
cols[j] = true;
119+
}
120+
}
121+
}
122+
for (int i = 0; i < m; ++i) {
123+
for (int j = 0; j < n; ++j) {
124+
if (rows[i] || cols[j]) {
125+
matrix[i][j] = 0;
98126
}
99127
}
100128
}
129+
}
130+
}
131+
```
101132

102-
// 行清零
103-
for (int row : zeroRows) {
104-
for (int j = 0; j < cols; ++j) {
105-
matrix[row][j] = 0;
133+
```java
134+
class Solution {
135+
public void setZeroes(int[][] matrix) {
136+
int m = matrix.length, n = matrix[0].length;
137+
boolean i0 = false, j0 = false;
138+
for (int j = 0; j < n; ++j) {
139+
if (matrix[0][j] == 0) {
140+
i0 = true;
141+
break;
142+
}
143+
}
144+
for (int i = 0; i < m; ++i) {
145+
if (matrix[i][0] == 0) {
146+
j0 = true;
147+
break;
148+
}
149+
}
150+
for (int i = 1; i < m; ++i) {
151+
for (int j = 1; j < n; ++j) {
152+
if (matrix[i][j] == 0) {
153+
matrix[i][0] = 0;
154+
matrix[0][j] = 0;
155+
}
156+
}
157+
}
158+
for (int i = 1; i < m; ++i) {
159+
for (int j = 1; j < n; ++j) {
160+
if (matrix[i][0] == 0 || matrix[0][j] == 0) {
161+
matrix[i][j] = 0;
162+
}
163+
}
164+
}
165+
if (i0) {
166+
for (int j = 0; j < n; ++j) {
167+
matrix[0][j] = 0;
168+
}
169+
}
170+
if (j0) {
171+
for (int i = 0; i < m; ++i) {
172+
matrix[i][0] = 0;
173+
}
174+
}
175+
}
176+
}
177+
```
178+
179+
### **C++**
180+
181+
```cpp
182+
class Solution {
183+
public:
184+
void setZeroes(vector<vector<int>>& matrix) {
185+
int m = matrix.size(), n = matrix[0].size();
186+
vector<bool> rows(m);
187+
vector<bool> cols(n);
188+
for (int i = 0; i < m; ++i) {
189+
for (int j = 0; j < n; ++j) {
190+
if (!matrix[i][j]) {
191+
rows[i] = 1;
192+
cols[j] = 1;
193+
}
194+
}
195+
}
196+
for (int i = 0; i < m; ++i) {
197+
for (int j = 0; j < n; ++j) {
198+
if (rows[i] || cols[j]) {
199+
matrix[i][j] = 0;
200+
}
106201
}
107202
}
203+
}
204+
};
205+
```
108206
109-
// 列清零
110-
for (int col : zeroCols) {
111-
for (int i = 0; i < rows; ++i) {
112-
matrix[i][col] = 0;
207+
```cpp
208+
class Solution {
209+
public:
210+
void setZeroes(vector<vector<int>>& matrix) {
211+
int m = matrix.size(), n = matrix[0].size();
212+
bool i0 = false, j0 = false;
213+
for (int j = 0; j < n; ++j) {
214+
if (matrix[0][j] == 0) {
215+
i0 = true;
216+
break;
217+
}
218+
}
219+
for (int i = 0; i < m; ++i) {
220+
if (matrix[i][0] == 0) {
221+
j0 = true;
222+
break;
223+
}
224+
}
225+
for (int i = 1; i < m; ++i) {
226+
for (int j = 1; j < n; ++j) {
227+
if (matrix[i][j] == 0) {
228+
matrix[i][0] = 0;
229+
matrix[0][j] = 0;
230+
}
231+
}
232+
}
233+
for (int i = 1; i < m; ++i) {
234+
for (int j = 1; j < n; ++j) {
235+
if (matrix[i][0] == 0 || matrix[0][j] == 0) {
236+
matrix[i][j] = 0;
237+
}
238+
}
239+
}
240+
if (i0) {
241+
for (int j = 0; j < n; ++j) {
242+
matrix[0][j] = 0;
243+
}
244+
}
245+
if (j0) {
246+
for (int i = 0; i < m; ++i) {
247+
matrix[i][0] = 0;
113248
}
114249
}
115250
}
251+
};
252+
```
253+
254+
### **Go**
255+
256+
```go
257+
func setZeroes(matrix [][]int) {
258+
m, n := len(matrix), len(matrix[0])
259+
rows := make([]bool, m)
260+
cols := make([]bool, n)
261+
for i, row := range matrix {
262+
for j, v := range row {
263+
if v == 0 {
264+
rows[i] = true
265+
cols[j] = true
266+
}
267+
}
268+
}
269+
for i := 0; i < m; i++ {
270+
for j := 0; j < n; j++ {
271+
if rows[i] || cols[j] {
272+
matrix[i][j] = 0
273+
}
274+
}
275+
}
276+
}
277+
```
278+
279+
```go
280+
func setZeroes(matrix [][]int) {
281+
m, n := len(matrix), len(matrix[0])
282+
i0, j0 := false, false
283+
for j := 0; j < n; j++ {
284+
if matrix[0][j] == 0 {
285+
i0 = true
286+
break
287+
}
288+
}
289+
for i := 0; i < m; i++ {
290+
if matrix[i][0] == 0 {
291+
j0 = true
292+
break
293+
}
294+
}
295+
for i := 1; i < m; i++ {
296+
for j := 1; j < n; j++ {
297+
if matrix[i][j] == 0 {
298+
matrix[i][0], matrix[0][j] = 0, 0
299+
}
300+
}
301+
}
302+
for i := 1; i < m; i++ {
303+
for j := 1; j < n; j++ {
304+
if matrix[i][0] == 0 || matrix[0][j] == 0 {
305+
matrix[i][j] = 0
306+
}
307+
}
308+
}
309+
if i0 {
310+
for j := 0; j < n; j++ {
311+
matrix[0][j] = 0
312+
}
313+
}
314+
if j0 {
315+
for i := 0; i < m; i++ {
316+
matrix[i][0] = 0
317+
}
318+
}
116319
}
117320
```
118321

@@ -124,30 +327,72 @@ class Solution {
124327
* @return {void} Do not return anything, modify matrix in-place instead.
125328
*/
126329
var setZeroes = function (matrix) {
127-
let m = matrix.length,
128-
n = matrix[0].length;
129-
let rows = new Array(m).fill(false);
130-
let cols = new Array(n).fill(false);
131-
// 标记
132-
for (let i = 0; i < m; i++) {
133-
for (let j = 0; j < n; j++) {
330+
const m = matrix.length;
331+
const n = matrix[0].length;
332+
const rows = new Array(m).fill(false);
333+
const cols = new Array(n).fill(false);
334+
for (let i = 0; i < m; ++i) {
335+
for (let j = 0; j < n; ++j) {
134336
if (matrix[i][j] == 0) {
135337
rows[i] = true;
136338
cols[j] = true;
137339
}
138340
}
139341
}
140-
// 清零
141-
for (let i = 0; i < m; i++) {
142-
for (let j = 0; j < n; j++) {
143-
if (matrix[i][j] != 0 && (rows[i] || cols[j])) {
342+
for (let i = 0; i < m; ++i) {
343+
for (let j = 0; j < n; ++j) {
344+
if (rows[i] || cols[j]) {
144345
matrix[i][j] = 0;
145346
}
146347
}
147348
}
148349
};
149350
```
150351

352+
```js
353+
/**
354+
* @param {number[][]} matrix
355+
* @return {void} Do not return anything, modify matrix in-place instead.
356+
*/
357+
var setZeroes = function (matrix) {
358+
const m = matrix.length;
359+
const n = matrix[0].length;
360+
let i0 = matrix[0].some(v => v == 0);
361+
let j0 = false;
362+
for (let i = 0; i < m; ++i) {
363+
if (matrix[i][0] == 0) {
364+
j0 = true;
365+
break;
366+
}
367+
}
368+
for (let i = 1; i < m; ++i) {
369+
for (let j = 1; j < n; ++j) {
370+
if (matrix[i][j] == 0) {
371+
matrix[i][0] = 0;
372+
matrix[0][j] = 0;
373+
}
374+
}
375+
}
376+
for (let i = 1; i < m; ++i) {
377+
for (let j = 1; j < n; ++j) {
378+
if (matrix[i][0] == 0 || matrix[0][j] == 0) {
379+
matrix[i][j] = 0;
380+
}
381+
}
382+
}
383+
if (i0) {
384+
for (let j = 0; j < n; ++j) {
385+
matrix[0][j] = 0;
386+
}
387+
}
388+
if (j0) {
389+
for (let i = 0; i < m; ++i) {
390+
matrix[i][0] = 0;
391+
}
392+
}
393+
};
394+
```
395+
151396
### **...**
152397

153398
```

0 commit comments

Comments
 (0)