File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change @@ -179,6 +179,48 @@ var setZeroes = function(matrix) {
179179};
180180```
181181Python3 Code:
182+
183+ 直接修改第一行和第一列为0的解法:
184+ ``` python
185+ class Solution :
186+ def setZeroes (self , matrix : List[List[int ]]) -> None :
187+ """
188+ Do not return anything, modify matrix in-place instead.
189+ """
190+ def setRowZeros (matrix : List[List[int ]], i :int ) -> None :
191+ C = len (matrix[0 ])
192+ matrix[i] = [0 ] * C
193+
194+ def setColZeros (matrix : List[List[int ]], j :int ) -> None :
195+ R = len (matrix)
196+ for i in range (R):
197+ matrix[i][j] = 0
198+
199+ isCol = False
200+ R = len (matrix)
201+ C = len (matrix[0 ])
202+
203+ for i in range (R):
204+ if matrix[i][0 ] == 0 :
205+ isCol = True
206+ for j in range (1 , C):
207+ if matrix[i][j] == 0 :
208+ matrix[i][0 ] = 0
209+ matrix[0 ][j] = 0
210+ for j in range (1 , C):
211+ if matrix[0 ][j] == 0 :
212+ setColZeros(matrix, j)
213+
214+ for i in range (R):
215+ if matrix[i][0 ] == 0 :
216+ setRowZeros(matrix, i)
217+
218+ if isCol:
219+ setColZeros(matrix, 0 )
220+
221+ ```
222+
223+ 另一种方法是用一个特殊符合标记需要改变的结果,只要这个特殊标记不在我们的题目数据范围(0和1)即可,这里用None。
182224``` python
183225class Solution :
184226 def setZeroes (self , matrix : List[List[int ]]) -> None :
You can’t perform that action at this time.
0 commit comments