File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change @@ -115,6 +115,8 @@ var setZeroes = function(matrix) {
115115- 最后更新第一行第一列
116116## 代码
117117
118+ * 语言支持:JS,Python3
119+
118120``` js
119121/*
120122 * @lc app=leetcode id=73 lang=javascript
@@ -176,6 +178,33 @@ var setZeroes = function(matrix) {
176178 return matrix;
177179};
178180```
181+ Python3 Code:
182+ ``` python
183+ class Solution :
184+ def setZeroes (self , matrix : List[List[int ]]) -> None :
185+ """
186+ 这题要解决的问题是,必须有个地方记录判断结果,但又不能影响下一步的判断条件;
187+ 直接改为0的话,会影响下一步的判断条件;
188+ 因此,有一种思路是先改为None,最后再将None改为0;
189+ 从条件上看,如果可以将第一行、第二行作为记录空间,那么,用None应该也不算违背题目条件;
190+ """
191+ rows = len (matrix)
192+ cols = len (matrix[0 ])
193+ # 遍历矩阵,用None记录要改的地方,注意如果是0则要保留,否则会影响下一步判断
194+ for r in range (rows):
195+ for c in range (cols):
196+ if matrix[r][c] is not None and matrix[r][c] == 0 :
197+ # 改值
198+ for i in range (rows):
199+ matrix[i][c] = None if matrix[i][c] != 0 else 0
200+ for j in range (cols):
201+ matrix[r][j] = None if matrix[r][j] != 0 else 0
202+ # 再次遍历,将None改为0
203+ for r in range (rows):
204+ for c in range (cols):
205+ if matrix[r][c] is None :
206+ matrix[r][c] = 0
207+ ```
179208
180209## 扩展
181210
You can’t perform that action at this time.
0 commit comments