Set Matrix Zeroes

update Sep 28 2018, 0:04

LeetCodearrow-up-right

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

8Example 1:*

Input:
[
  [1,1,1],
  [1,0,1],
  [1,1,1]
]
Output:
[
  [1,0,1],
  [0,0,0],
  [1,0,1]
]

Example 2:

Input:
[
  [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]
]

Follow up:

  1. A straight forward solution using O(mn) space is probably a bad idea.

  2. A simple improvement uses O(m + n) space, but still not the best solution. Could you devise a constant space solution?

Basic Idea:

直接考虑follow up中的空间优化,我们可以利用最上面的row来记录哪些col需要清零,另外用一个boolean变量记录最上面的row本身是否需要清零。从上到下逐行扫描,完成一行之后,将0出现的col标记在第0 row,然后清空当前row。

Java Code: