Problem

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.

https://leetcode.com/problems/set-matrix-zeroes/

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:

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
  • -2^31 <= matrix[i][j] <= 2^31 - 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?

Test Cases

1
2
3
4
5
class Solution:
def setZeroes(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
solution_test.py
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
import copy

import pytest

from solution import Solution
from solution2 import Solution as Solution2


@pytest.mark.parametrize('matrix, expected', [
([[1,1,1],[1,0,1],[1,1,1]], [[1,0,1],[0,0,0],[1,0,1]]),
([[0,1,2,0],[3,4,5,2],[1,3,1,5]], [[0,0,0,0],[0,4,5,0],[0,3,1,0]]),

([[1,2,3,4],[5,0,7,8],[0,10,11,12],[13,14,15,0]], [[0,0,3,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]),
])
class Test:
def test_solution(self, matrix, expected):
sol = Solution()
matrix = copy.deepcopy(matrix)
sol.setZeroes(matrix)
assert matrix == expected

def test_solution2(self, matrix, expected):
sol = Solution2()
matrix = copy.deepcopy(matrix)
sol.setZeroes(matrix)
assert matrix == expected

Thoughts

用长为 m 的数组记录哪一行需要换成 0,长为 n 的数组记录哪一列需要换成 0

遍历一遍 grid,如果 grid[i][j]0,则标记 i 行和 j 列。

再遍历一遍 grid,根据行和列的标记,将需要替换的格子的值改成 0

时间复杂度 O(mn),空间复杂度 O(m+n)

Code

solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution:
def setZeroes(self, matrix: list[list[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
m = len(matrix)
n = len(matrix[0])
rows = [False] * m
cols = [False] * n

for i, row in enumerate(matrix):
for j, v in enumerate(row):
if v == 0:
rows[i] = True
cols[j] = True

for i in range(m):
for j in range(n):
if rows[i] or cols[j]:
matrix[i][j] = 0

Follow Up

可以直接用 matrix 的第一行和第一列记录需要全部换成 0 的列和行。唯一需要注意的是 matrix[0][0] 是公共的元素,为了避免冲突,可以用单独的变量记录第一行和第一列是否需要换。

solution2.py
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
class Solution:
def setZeroes(self, matrix: list[list[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
m = len(matrix)
n = len(matrix[0])
first_row = False
first_col = False

for i in range(m):
if matrix[i][0] == 0:
first_col = True
break

for j in range(n):
if matrix[0][j] == 0:
first_row = True
break

for i in range(1, m):
for j in range(1, n):
if matrix[i][j] == 0:
matrix[i][0] = 0
matrix[0][j] = 0

for i in range(1, m):
for j in range(1, n):
if matrix[i][0] == 0 or matrix[0][j] == 0:
matrix[i][j] = 0

if first_col:
for i in range(m):
matrix[i][0] = 0

if first_row:
for j in range(n):
matrix[0][j] = 0