### 题目

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.

Follow up: Did you use extra space? A straight forward solution using O(mn) space is probably a bad idea. A simple improvement uses O(m + n) space, but still not the best solution. Could you devise a constant space solution?

### 先记下要变零的行号，列号，$O(m+n)$ space

#### 代码

public class Solution {
public void setZeroes(int[][] matrix) {
int lineSize = matrix.length;
if (lineSize == 0) { return; }
int columnSize = matrix[0].length;
List<Integer> zeroLine = new ArrayList<>();
List<Integer> zeroColumn = new ArrayList<>();
for (int i = 0; i < lineSize; i++) {
for (int j = 0; j < columnSize; j++) {
if (matrix[i][j] == 0) {
}
}
}
for (int num : zeroLine) {
for (int i = 0; i < columnSize; i++) {
matrix[num][i] = 0;
}
}
for (int num : zeroColumn) {
for (int i = 0; i < lineSize; i++) {
matrix[i][num] = 0;
}
}
}
}


### 用第一行和第一列记录需要变0的行列信息. $O(1)$ space

1	0	3	4	5

1	2	3	4	5

6	7	8	9	0

1	3	5	7	9


// 注意，第一行有0，不在第一列标记。用一个boolean firstLineShouldFillZero标记

1	0	3	4	0  // firstLineShouldFillZero = true

1	2	3	4	5

0	7	8	9	0

1	3	5	7	9


#### 代码

public void setZeroes(int[][] matrix) {
int lineSize = matrix.length;
if (lineSize == 0) { return; }
int columnSize = matrix[0].length;
boolean firstLineShouldFillZero = false;
for (int i = 0; i < lineSize; i++) {
for (int j = 0; j < columnSize; j++) {
if (matrix[i][j] == 0) { // 标记，此行回头应该整行变0.
if (i == 0) {
firstLineShouldFillZero = true; // 第一行比较特殊，用firstLineShouldFillZero来标记
} else {
matrix[i][0] = 0;
}
matrix[0][j] = 0; // 标记，当全部遍历完，回过头把整个这一列变0.
}
}
}
for (int i = 1; i < lineSize; i++) { // 根据第一列，把需要变0的行全变0。跳过第一行
if (matrix[i][0] == 0) {
for (int j = 0; j < columnSize; j++) {
matrix[i][j] = 0;
}
}
}
for (int i = 0; i < columnSize; i++) { // 根据第一行，把需要变0的列，全变0.
if (matrix[0][i] == 0) {
for (int j = 0; j < lineSize; j++) {
matrix[j][i] = 0;
}
}
}
if (firstLineShouldFillZero) { // 根据firstLineShouldFillZero决定第一行要不要全变0
for (int i = 0; i < columnSize; i++) {
matrix[0][i] = 0;
}
}
}


#### 代码整理

public class Solution {
public void setZeroes(int[][] matrix) {
if (matrix.length == 0) { return; }
int lineSize = matrix.length, columnSize = matrix[0].length;
boolean firstLineShouldFillZero = false, firstColumnShouldFillZero = false;
for (int i = 0; i < lineSize; i++) {
for (int j = 0; j < columnSize; j++) {
if (matrix[i][j] == 0) {
if (i == 0) { firstLineShouldFillZero = true; }
if (j == 0) { firstColumnShouldFillZero = true; }
matrix[0][j] = 0;
matrix[i][0] = 0;
}
}
}
for (int i = 1; i < lineSize; i++) { // 根据第一行，第一列，把需要变0的行全变0。跳过第一行，第一列
for (int j = 1; j < columnSize; j++) {
if (matrix[i][0] == 0 || matrix[0][j] == 0) {
matrix[i][j] = 0;
}
}
}
if (firstLineShouldFillZero) { // 根据firstLineShouldFillZero决定第一行要不要全变0
for (int i = 0; i < columnSize; i++) {
matrix[0][i] = 0;
}
}
if (firstColumnShouldFillZero) { // 根据firstColumnShouldFillZero决定第一列要不要全变0
for (int i = 0; i < lineSize; i++) {
matrix[i][0] = 0;
}
}
}
}