Skip to content

Commit d79b55a

Browse files
Merge pull request SharingSource#694 from SharingSource/ac_oier
✨feat: add 面试题 01.08
2 parents 7c78959 + cefa0a9 commit d79b55a

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

Index/模拟.md

+1
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@
207207
| [2069. 模拟行走机器人 II](https://leetcode-cn.com/problems/walking-robot-simulation-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/walking-robot-simulation-ii/solution/by-ac_oier-6zib/) | 中等 | 🤩🤩🤩🤩 |
208208
| [面试题 01.02. 判定是否互为字符重排](https://leetcode.cn/problems/check-permutation-lcci/) | [LeetCode 题解链接](https://leetcode.cn/problems/check-permutation-lcci/solution/by-ac_oier-qj3j/) | 简单 | 🤩🤩🤩 |
209209
| [面试题 01.05. 一次编辑](https://leetcode.cn/problems/one-away-lcci/) | [LeetCode 题解链接](https://leetcode.cn/problems/one-away-lcci/solution/by-ac_oier-7ml0/) | 中等 | 🤩🤩🤩🤩 |
210+
| [面试题 01.08. 零矩阵](https://leetcode.cn/problems/zero-matrix-lcci/) | [LeetCode 题解链接](https://leetcode.cn/problems/zero-matrix-lcci/solution/by-ac_oier-0lo0/) | 中等 | 🤩🤩🤩🤩 |
210211
| [面试题 10.02. 变位词组](https://leetcode-cn.com/problems/group-anagrams-lcci/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/group-anagrams-lcci/solution/gong-shui-san-xie-tong-ji-bian-wei-ci-de-0iqe/) | 中等 | 🤩🤩🤩🤩 |
211212
| [面试题 17.11. 单词距离](https://leetcode.cn/problems/find-closest-lcci/) | [LeetCode 题解链接](https://leetcode.cn/problems/find-closest-lcci/solution/by-ac_oier-0hv9/) | 中等 | 🤩🤩🤩🤩 |
212213
| [剑指 Offer II 003. 前 n 个数字二进制中 1 的个数](https://leetcode.cn/problems/w3tCBm/) | [LeetCode 题解链接](https://leetcode.cn/problems/w3tCBm/solution/by-ac_oier-cnlt/) | 简单 | 🤩🤩🤩 |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[面试题 01.08. 零矩阵](https://leetcode.cn/problems/zero-matrix-lcci/solution/by-ac_oier-0lo0/)** ,难度为 **中等**
4+
5+
Tag : 「模拟」
6+
7+
8+
9+
编写一种算法,若 $M \times N$ 矩阵中某个元素为 $0$,则将其所在的行与列清零。
10+
11+
示例 1:
12+
```
13+
输入:
14+
[
15+
[1,1,1],
16+
[1,0,1],
17+
[1,1,1]
18+
]
19+
20+
输出:
21+
[
22+
[1,0,1],
23+
[0,0,0],
24+
[1,0,1]
25+
]
26+
```
27+
示例 2:
28+
```
29+
输入:
30+
[
31+
[0,1,2,0],
32+
[3,4,5,2],
33+
[1,3,1,5]
34+
]
35+
36+
输出:
37+
[
38+
[0,0,0,0],
39+
[0,4,5,0],
40+
[0,3,1,0]
41+
]
42+
```
43+
44+
---
45+
46+
### 模拟
47+
48+
根据题意进行模拟。
49+
50+
Java 代码:
51+
```Java
52+
class Solution {
53+
public void setZeroes(int[][] mat) {
54+
int n = mat.length, m = mat[0].length;
55+
boolean[] rows = new boolean[n], cols = new boolean[m];
56+
for (int i = 0; i < n; i++) {
57+
for (int j = 0; j < m; j++) {
58+
if (mat[i][j] == 0) rows[i] = cols[j] = true;
59+
}
60+
}
61+
for (int i = 0; i < n; i++) {
62+
for (int j = 0; j < m; j++) {
63+
if (rows[i] || cols[j]) mat[i][j] = 0;
64+
}
65+
}
66+
}
67+
}
68+
```
69+
TypeScript 代码:
70+
```TypeScript
71+
function setZeroes(mat: number[][]): void {
72+
const n = mat.length, m = mat[0].length
73+
const rows = new Array<boolean>(n).fill(false), cols = new Array<boolean>(m).fill(false)
74+
for (let i = 0; i < n; i++) {
75+
for (let j = 0; j < m; j++) {
76+
if (mat[i][j] == 0) rows[i] = cols[j] = true
77+
}
78+
}
79+
for (let i = 0; i < n; i++) {
80+
for (let j = 0; j < m; j++) {
81+
if (rows[i] || cols[j]) mat[i][j] = 0
82+
}
83+
}
84+
};
85+
```
86+
Python 代码:
87+
```Python
88+
class Solution:
89+
def setZeroes(self, mat: List[List[int]]) -> None:
90+
n, m = len(mat), len(mat[0])
91+
rows, cols = [False] * n, [False] * m
92+
for i in range(n):
93+
for j in range(m):
94+
if mat[i][j] == 0:
95+
rows[i] = cols[j] = True
96+
for i in range(n):
97+
for j in range(m):
98+
mat[i][j] = 0 if rows[i] or cols[j] else mat[i][j]
99+
```
100+
* 时间复杂度:$O(n \times m)$
101+
* 空间复杂度:$O(n + m)$
102+
103+
---
104+
105+
### 最后
106+
107+
这是我们「刷穿 LeetCode」系列文章的第 `No.面试题 01.08` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
108+
109+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
110+
111+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
112+
113+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
114+

0 commit comments

Comments
 (0)