[LeetCode] 73. Set Matrix Zeroes

Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0’s.

You must do it in place.

Example 1:
Example 1
Input: matrix = [[1,1,1],[1,0,1],[1,1,1]]
Output: [[1,0,1],[0,0,0],[1,0,1]]

Example 2:
Example 2
Input: matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]
Output: [[0,0,0,0],[0,4,5,0],[0,3,1,0]]

Constraints:
m == matrix.length
n == matrix[0].length
1 <= m, n <= 200
-231 <= matrix[i][j] <= 231 - 1

Follow up:
A straightforward 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?

矩阵置零。

给定一个 m x n 的矩阵,如果一个元素为 0 ,则将其所在行和列的所有元素都设为 0 。请使用 原地 算法。

思路

数组/矩阵类型的题有好几道都明确要求不能使用额外空间,这题也不例外。因为不能用到额外空间,所以只能将是否需要赋零的flag记录在input的数组里面,这里我选择记录在第一行和第一列上。但是我此时仍然需要两个flag来分别记录第一行和第一列上有没有0。因为第一行和第一列上的0有可能是因着记录里面的行和列的结果而被赋值成0的。我参考了grandyang大神的思路,非常简洁,列在这里。

  • 先扫描第一行第一列,如果有0,则将各自的flag设置为true
  • 然后扫描除去第一行第一列的整个数组,如果有0,则将对应的第一行和第一列的数字赋0
  • 再次遍历除去第一行第一列的整个数组,如果对应的第一行和第一列的数字有一个为0,则将当前值赋0
  • 最后根据第一行第一列的flag来更新第一行第一列

复杂度

时间O(mn)
空间O(1)

代码

Java实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class Solution {
public void setZeroes(int[][] matrix) {
// corner case
if (matrix == null || matrix.length == 0) {
return;
}

// normal case
int m = matrix.length;
int n = matrix[0].length;
boolean row = false;
boolean col = false;
// mark if first row and first col has zero
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i][j] == 0) {
matrix[i][0] = matrix[0][j] = 0;
if (i == 0) row = true;
if (j == 0) col = true;
}
}
}

// change the inner part to zeroes based on the first row and first col
for (int i = 1; i < m; i++) {
if (matrix[i][0] == 0) {
for (int j = 1; j < n; j++) {
matrix[i][j] = 0;
}
}
}
for (int j = 1; j < n; j++) {
if (matrix[0][j] == 0) {
for (int i = 1; i < m; i++) {
matrix[i][j] = 0;
}
}
}

// change the first row and first col if needed
if (row) {
for (int j = 0; j < n; j++) {
matrix[0][j] = 0;
}
}
if (col) {
for (int i = 0; i < m; i++) {
matrix[i][0] = 0;
}
}
}
}

JavaScript实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* @param {number[][]} matrix
* @return {void} Do not return anything, modify matrix in-place instead.
*/
var setZeroes = function (matrix) {
// corner case
if (matrix.length === 0 || matrix === null) return;

// normal case
let m = matrix.length
let n = matrix[0].length;
let rowZero = false;
let colZero = false;
// check if first row has zero
for (let i = 0; i < m; i++) {
if (matrix[i][0] === 0) colZero = true;
}

// check if first column has zero
for (let i = 0; i < n; i++) {
if (matrix[0][i] === 0) rowZero = true;
}

// scan all the other rows and columns
for (let i = 1; i < m; i++) {
for (let j = 1; j < n; j++) {
if (matrix[i][j] === 0) {
matrix[0][j] = 0;
matrix[i][0] = 0;
}
}
}

// scan again to see if we should mark the whole row or column to be zero
for (let i = 1; i < m; i++) {
for (let j = 1; j < n; j++) {
if (matrix[i][0] === 0 || matrix[0][j] === 0) {
matrix[i][j] = 0;
}
}
}

// check if we should mark the first row and first column to be zero
if (rowZero) {
for (let i = 0; i < n; i++) matrix[0][i] = 0;
}
if (colZero) {
for (let i = 0; i < m; i++) matrix[i][0] = 0;
}
};

相关题目

1
2
73. Set Matrix Zeroes
289. Game of Life

[LeetCode] 73. Set Matrix Zeroes
https://shurui91.github.io/posts/4041800823.html
Author
Aaron Liu
Posted on
February 14, 2020
Licensed under