Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem73.python #193

Merged
merged 4 commits into from
Sep 30, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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