Skip to content

Commit 51f555a

Browse files
committed
feat: add python and java solutions to lcci problem
添加《程序员面试金典》题解:面试题 01.08. 零矩阵
1 parent c9acc9c commit 51f555a

File tree

4 files changed

+252
-63
lines changed

4 files changed

+252
-63
lines changed

lcci/01.08.Zero Matrix/README.md

+54-1
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,72 @@
4242
## 解法
4343
<!-- 这里可写通用的实现逻辑 -->
4444

45+
用 set 记录需要清零的行 `zero_rows` 跟列 `zero_cols`,之后分别将需要清零的行、列上的所有元素清零。
4546

4647
### Python3
4748
<!-- 这里可写当前语言的特殊实现逻辑 -->
4849

4950
```python
51+
class Solution:
52+
def setZeroes(self, matrix: List[List[int]]) -> None:
53+
"""
54+
Do not return anything, modify matrix in-place instead.
55+
"""
56+
rows, cols = len(matrix), len(matrix[0])
57+
zero_rows, zero_cols = set(), set()
58+
for i in range(rows):
59+
for j in range(cols):
60+
if matrix[i][j] == 0:
61+
zero_rows.add(i)
62+
zero_cols.add(j)
63+
64+
# 行清零
65+
for i in zero_rows:
66+
for j in range(cols):
67+
matrix[i][j] = 0
68+
69+
# 列清零
70+
for j in zero_cols:
71+
for i in range(rows):
72+
matrix[i][j] = 0
73+
74+
return matrix
5075

5176
```
5277

5378
### Java
5479
<!-- 这里可写当前语言的特殊实现逻辑 -->
5580

5681
```java
57-
82+
class Solution {
83+
public void setZeroes(int[][] matrix) {
84+
int rows = matrix.length, cols = matrix[0].length;
85+
Set<Integer> zeroRows = new HashSet<>();
86+
Set<Integer> zeroCols = new HashSet<>();
87+
for (int i = 0; i < rows; ++i) {
88+
for (int j = 0; j < cols; ++j) {
89+
if (matrix[i][j] == 0) {
90+
zeroRows.add(i);
91+
zeroCols.add(j);
92+
}
93+
}
94+
}
95+
96+
// 行清零
97+
for (int row : zeroRows) {
98+
for (int j = 0; j < cols; ++j) {
99+
matrix[row][j] = 0;
100+
}
101+
}
102+
103+
// 列清零
104+
for (int col : zeroCols) {
105+
for (int i = 0; i < rows; ++i) {
106+
matrix[i][col] = 0;
107+
}
108+
}
109+
}
110+
}
58111
```
59112

60113
### ...

lcci/01.08.Zero Matrix/README_EN.md

+147-62
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,147 @@
1-
# [01.08. Zero Matrix](https://leetcode-cn.com/problems/zero-matrix-lcci)
2-
3-
## Description
4-
<p>Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column are set to 0.</p>
5-
6-
<p>&nbsp;</p>
7-
8-
<p><strong>Example 1: </strong></p>
9-
10-
<pre>
11-
<strong>Input: </strong>
12-
[
13-
[1,1,1],
14-
[1,0,1],
15-
[1,1,1]
16-
]
17-
<strong>Output: </strong>
18-
[
19-
[1,0,1],
20-
[0,0,0],
21-
[1,0,1]
22-
]
23-
</pre>
24-
25-
<p><strong>Example 2: </strong></p>
26-
27-
<pre>
28-
<strong>Input: </strong>
29-
[
30-
[0,1,2,0],
31-
[3,4,5,2],
32-
[1,3,1,5]
33-
]
34-
<strong>Output: </strong>
35-
[
36-
[0,0,0,0],
37-
[0,4,5,0],
38-
[0,3,1,0]
39-
]
40-
</pre>
41-
42-
43-
44-
## Solutions
45-
46-
47-
### Python3
48-
49-
```python
50-
51-
```
52-
53-
### Java
54-
55-
```java
56-
57-
```
58-
59-
### ...
60-
```
61-
62-
```
1+
# [01.08. Zero Matrix](https://leetcode-cn.com/problems/zero-matrix-lcci)
2+
3+
## Description
4+
<p>Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column are set to 0.</p>
5+
6+
7+
8+
<p>&nbsp;</p>
9+
10+
11+
12+
<p><strong>Example 1: </strong></p>
13+
14+
15+
16+
<pre>
17+
18+
<strong>Input: </strong>
19+
20+
[
21+
22+
[1,1,1],
23+
24+
[1,0,1],
25+
26+
[1,1,1]
27+
28+
]
29+
30+
<strong>Output: </strong>
31+
32+
[
33+
34+
[1,0,1],
35+
36+
[0,0,0],
37+
38+
[1,0,1]
39+
40+
]
41+
42+
</pre>
43+
44+
45+
46+
<p><strong>Example 2: </strong></p>
47+
48+
49+
50+
<pre>
51+
52+
<strong>Input: </strong>
53+
54+
[
55+
56+
[0,1,2,0],
57+
58+
[3,4,5,2],
59+
60+
[1,3,1,5]
61+
62+
]
63+
64+
<strong>Output: </strong>
65+
66+
[
67+
68+
[0,0,0,0],
69+
70+
[0,4,5,0],
71+
72+
[0,3,1,0]
73+
74+
]
75+
76+
</pre>
77+
78+
79+
80+
81+
## Solutions
82+
83+
84+
### Python3
85+
86+
```python
87+
class Solution:
88+
def setZeroes(self, matrix: List[List[int]]) -> None:
89+
"""
90+
Do not return anything, modify matrix in-place instead.
91+
"""
92+
rows, cols = len(matrix), len(matrix[0])
93+
zero_rows, zero_cols = set(), set()
94+
for i in range(rows):
95+
for j in range(cols):
96+
if matrix[i][j] == 0:
97+
zero_rows.add(i)
98+
zero_cols.add(j)
99+
100+
for i in zero_rows:
101+
for j in range(cols):
102+
matrix[i][j] = 0
103+
104+
for j in zero_cols:
105+
for i in range(rows):
106+
matrix[i][j] = 0
107+
108+
return matrix
109+
110+
```
111+
112+
### Java
113+
114+
```java
115+
class Solution {
116+
public void setZeroes(int[][] matrix) {
117+
int rows = matrix.length, cols = matrix[0].length;
118+
Set<Integer> zeroRows = new HashSet<>();
119+
Set<Integer> zeroCols = new HashSet<>();
120+
for (int i = 0; i < rows; ++i) {
121+
for (int j = 0; j < cols; ++j) {
122+
if (matrix[i][j] == 0) {
123+
zeroRows.add(i);
124+
zeroCols.add(j);
125+
}
126+
}
127+
}
128+
129+
for (int row : zeroRows) {
130+
for (int j = 0; j < cols; ++j) {
131+
matrix[row][j] = 0;
132+
}
133+
}
134+
135+
for (int col : zeroCols) {
136+
for (int i = 0; i < rows; ++i) {
137+
matrix[i][col] = 0;
138+
}
139+
}
140+
}
141+
}
142+
```
143+
144+
### ...
145+
```
146+
147+
```

lcci/01.08.Zero Matrix/Solution.java

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public void setZeroes(int[][] matrix) {
3+
int rows = matrix.length, cols = matrix[0].length;
4+
Set<Integer> zeroRows = new HashSet<>();
5+
Set<Integer> zeroCols = new HashSet<>();
6+
for (int i = 0; i < rows; ++i) {
7+
for (int j = 0; j < cols; ++j) {
8+
if (matrix[i][j] == 0) {
9+
zeroRows.add(i);
10+
zeroCols.add(j);
11+
}
12+
}
13+
}
14+
15+
// 行清零
16+
for (int row : zeroRows) {
17+
for (int j = 0; j < cols; ++j) {
18+
matrix[row][j] = 0;
19+
}
20+
}
21+
22+
// 列清零
23+
for (int col : zeroCols) {
24+
for (int i = 0; i < rows; ++i) {
25+
matrix[i][col] = 0;
26+
}
27+
}
28+
}
29+
}

lcci/01.08.Zero Matrix/Solution.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution:
2+
def setZeroes(self, matrix: List[List[int]]) -> None:
3+
"""
4+
Do not return anything, modify matrix in-place instead.
5+
"""
6+
rows, cols = len(matrix), len(matrix[0])
7+
zero_rows, zero_cols = set(), set()
8+
for i in range(rows):
9+
for j in range(cols):
10+
if matrix[i][j] == 0:
11+
zero_rows.add(i)
12+
zero_cols.add(j)
13+
14+
for i in zero_rows:
15+
for j in range(cols):
16+
matrix[i][j] = 0
17+
18+
for j in zero_cols:
19+
for i in range(rows):
20+
matrix[i][j] = 0
21+
22+
return matrix

0 commit comments

Comments
 (0)