Skip to content

Commit

Permalink
feat: Problem73.python (azl397985856#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
kant-li authored and azl397985856 committed Sep 30, 2019
1 parent 5fed7c3 commit 5501460
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions problems/73.set-matrix-zeroes.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ var setZeroes = function(matrix) {
- 最后更新第一行第一列
## 代码

* 语言支持:JS,Python3

```js
/*
* @lc app=leetcode id=73 lang=javascript
Expand Down Expand Up @@ -176,6 +178,33 @@ var setZeroes = function(matrix) {
return matrix;
};
```
Python3 Code:
```python
class Solution:
def setZeroes(self, matrix: List[List[int]]) -> None:
"""
这题要解决的问题是,必须有个地方记录判断结果,但又不能影响下一步的判断条件;
直接改为0的话,会影响下一步的判断条件;
因此,有一种思路是先改为None,最后再将None改为0;
从条件上看,如果可以将第一行、第二行作为记录空间,那么,用None应该也不算违背题目条件;
"""
rows = len(matrix)
cols = len(matrix[0])
# 遍历矩阵,用None记录要改的地方,注意如果是0则要保留,否则会影响下一步判断
for r in range(rows):
for c in range(cols):
if matrix[r][c] is not None and matrix[r][c] == 0:
# 改值
for i in range(rows):
matrix[i][c] = None if matrix[i][c] != 0 else 0
for j in range(cols):
matrix[r][j] = None if matrix[r][j] != 0 else 0
# 再次遍历,将None改为0
for r in range(rows):
for c in range(cols):
if matrix[r][c] is None:
matrix[r][c] = 0
```

## 扩展

Expand Down

0 comments on commit 5501460

Please sign in to comment.