Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ba353fe

Browse files
add LeetCode 73. 矩阵置零
1 parent 8669c3d commit ba353fe

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
![](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9jZG4uanNkZWxpdnIubmV0L2doL2Nob2NvbGF0ZTE5OTkvY2RuL2ltZy8yMDIwMDgyODE0NTUyMS5qcGc?x-oss-process=image/format,png)
2+
>仰望星空的人,不应该被嘲笑
3+
4+
## 题目描述
5+
给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0。请使用原地算法。
6+
7+
示例 1:
8+
9+
```javascript
10+
输入:
11+
[
12+
[1,1,1],
13+
[1,0,1],
14+
[1,1,1]
15+
]
16+
输出:
17+
[
18+
[1,0,1],
19+
[0,0,0],
20+
[1,0,1]
21+
]
22+
```
23+
24+
示例 2:
25+
26+
```javascript
27+
输入:
28+
[
29+
[0,1,2,0],
30+
[3,4,5,2],
31+
[1,3,1,5]
32+
]
33+
输出:
34+
[
35+
[0,0,0,0],
36+
[0,4,5,0],
37+
[0,3,1,0]
38+
]
39+
```
40+
41+
进阶:
42+
43+
```javascript
44+
一个直接的解决方案是使用 O(mn) 的额外空间,但这并不是一个好的解决方案。
45+
一个简单的改进方案是使用 O(m + n) 的额外空间,但这仍然不是最好的解决方案。
46+
你能想出一个常数空间的解决方案吗?
47+
```
48+
49+
来源:力扣(LeetCode)
50+
链接:https://leetcode-cn.com/problems/set-matrix-zeroes
51+
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
52+
53+
54+
55+
56+
## 解题思路
57+
用 O(n) 空间复杂度来做,先遍历矩阵,找到等于0的坐标,然后遍历坐标,将对应行和列置为 0 即可
58+
59+
时间复杂度 O(m * n)
60+
61+
```javascript
62+
var setZeroes = function(matrix) {
63+
let n = matrix.length
64+
let m = matrix[0].length
65+
let arr = []
66+
for(let i=0;i<n;i++){
67+
for(let j=0;j<m;j++){
68+
if(matrix[i][j] == 0){
69+
arr.push([i,j])
70+
}
71+
}
72+
}
73+
while(arr.length){
74+
let [x,y] = arr.pop()
75+
for(let i=0;i<n;i++) matrix[i][y] = 0
76+
for(let j=0;j<m;j++) matrix[x][j] = 0
77+
}
78+
return matrix
79+
};
80+
```
81+
另外一种,**原地算法**,空间复杂度 O(1),我们无需借助外部空间。找到下标为 0 的坐标,然后直接对该行和该列不等于 0 的数字设置为 `-0` 即可。这里巧妙运用了 `JS` 中的 `Object.is()`方法,此时 `0`` -0` 不相等,但是最终返回的矩阵还是为 `0 `
82+
83+
```javascript
84+
var setZeroes = function(matrix) {
85+
for(let i=0;i<matrix.length;i++){
86+
for(let j=0;j<matrix[0].length;j++){
87+
if(Object.is(matrix[i][j],0)){
88+
// 对行进行操作
89+
for(let k=0;k<matrix.length;k++)
90+
if(!Object.is(matrix[k][j],0) && k!==i) matrix[k][j] = -0
91+
// 对列进行操作
92+
for(let k=0;k<matrix[0].length;k++)
93+
if(!Object.is(matrix[i][k],0) && k!==j) matrix[i][k] = -0
94+
}
95+
}
96+
}
97+
return matrix
98+
};
99+
```
100+
101+
102+
## 最后
103+
文章产出不易,还望各位小伙伴们支持一波!
104+
105+
往期精选:
106+
107+
<a href="https://github.com/Chocolate1999/Front-end-learning-to-organize-notes">小狮子前端の笔记仓库</a>
108+
109+
<a href="https://yangchaoyi.vip/">访问超逸の博客</a>,方便小伙伴阅读玩耍~
110+
111+
![](https://img-blog.csdnimg.cn/2020090211491121.png#pic_center)
112+
113+
```javascript
114+
学如逆水行舟,不进则退
115+
```
116+
117+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /