File tree 1 file changed +44
-0
lines changed
1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change
1
+
2
+ ## 题目
3
+ 编写一个算法,若M x N矩阵中某个元素为0,则将其所在的行与列清零。
4
+
5
+ ## 注解
6
+ 首先暴力解,遍历一次矩阵,把0的坐标记忆下来。然后将0所在的行列清零。
7
+ 用一个M x N的矩阵记忆有些浪费空间了。对我们而言,有用的只是(x,y)坐标键值对。可以使用一个二维数组(或者字典、元祖)。
8
+
9
+ 这道题还有Follow up
10
+
11
+ * 这道题在LeetCode上有。[ https://leetcode.com/problems/set-matrix-zeroes/description/ ] ( https://leetcode.com/problems/set-matrix-zeroes/description/ ) *
12
+
13
+
14
+ ``` python
15
+ class Solution (object ):
16
+ def setZeroes (self , matrix ):
17
+ """
18
+ :type matrix: List[List[int]]
19
+ :rtype: void Do not return anything, modify matrix in-place instead.
20
+ """
21
+
22
+ row_len = len (matrix)
23
+ if row_len == 0 :
24
+ return
25
+
26
+ col_len = len (matrix[0 ])
27
+ if col_len == 0 :
28
+ return
29
+
30
+ row = [0 for i in range (0 , col_len)]
31
+ col = [0 for i in range (0 , row_len)]
32
+
33
+ for i in range (0 , row_len):
34
+ for j in range (0 , col_len):
35
+ if matrix[i][j] == 0 :
36
+ col[i] = row[j] = 1
37
+
38
+ for i in range (0 , row_len):
39
+ for j in range (0 , col_len):
40
+ if col[i] or row[j]:
41
+ matrix[i][j] = 0
42
+
43
+
44
+ ```
You can’t perform that action at this time.
0 commit comments