[LeetCode] 73. Set Matrix Zeroes

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

Follow up:

  • 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?

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
  • -231 <= matrix[i][j] <= 231 - 1

矩阵置零。

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

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

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

时间O(mn)

空间O(1)

JavaScript实现

 1 /**
 2  * @param {number[][]} matrix
 3  * @return {void} Do not return anything, modify matrix in-place instead.
 4  */
 5 var setZeroes = function (matrix) {
 6     // corner case
 7     if (matrix.length === 0 || matrix === null) return;
 8 
 9     // normal case
10     let m = matrix.length
11     let n = matrix[0].length;
12     let rowZero = false;
13     let colZero = false;
14     // check if first row has zero
15     for (let i = 0; i < m; i++) {
16         if (matrix[i][0] === 0) colZero = true;
17     }
18 
19     // check if first column has zero
20     for (let i = 0; i < n; i++) {
21         if (matrix[0][i] === 0) rowZero = true;
22     }
23 
24     // scan all the other rows and columns
25     for (let i = 1; i < m; i++) {
26         for (let j = 1; j < n; j++) {
27             if (matrix[i][j] === 0) {
28                 matrix[0][j] = 0;
29                 matrix[i][0] = 0;
30             }
31         }
32     }
33 
34     // scan again to see if we should mark the whole row or column to be zero
35     for (let i = 1; i < m; i++) {
36         for (let j = 1; j < n; j++) {
37             if (matrix[i][0] === 0 || matrix[0][j] === 0) {
38                 matrix[i][j] = 0;
39             }
40         }
41     }
42 
43     // check if we should mark the first row and first column to be zero
44     if (rowZero) {
45         for (let i = 0; i < n; i++) matrix[0][i] = 0;
46     }
47     if (colZero) {
48         for (let i = 0; i < m; i++) matrix[i][0] = 0;
49     }
50 };

 

Java实现

 1 class Solution {
 2     public void setZeroes(int[][] matrix) {
 3         // corner case
 4         if (matrix == null || matrix.length == 0) {
 5             return;
 6         }
 7         
 8         // normal case
 9         int m = matrix.length;
10         int n = matrix[0].length;
11         boolean row = false;
12         boolean col = false;
13         // mark if first row and first col has zero
14         for (int i = 0; i < m; i++) {
15             for (int j = 0; j < n; j++) {
16                 if (matrix[i][j] == 0) {
17                     matrix[i][0] = matrix[0][j] = 0;
18                     if (i == 0) row = true;
19                     if (j == 0) col = true;
20                 }
21             }
22         }
23         
24         // change the inner part to zeroes based on the first row and first col
25         for (int i = 1; i < m; i++) {
26             if (matrix[i][0] == 0) {
27                 for (int j = 1; j < n; j++) {
28                     matrix[i][j] = 0;
29                 }
30             }
31         }
32         for (int j = 1; j < n; j++) {
33             if (matrix[0][j] == 0) {
34                 for (int i = 1; i < m; i++) {
35                     matrix[i][j] = 0;
36                 }
37             }
38         }
39         
40         // change the first row and first col if needed
41         if (row) {
42             for (int j = 0; j < n; j++) {
43                 matrix[0][j] = 0;
44             }
45         }
46         if (col) {
47             for (int i = 0; i < m; i++) {
48                 matrix[i][0] = 0;
49             }
50         }
51     }
52 }

 

相关题目

73. Set Matrix Zeroes

289. Game of Life

LeetCode 题目总结

posted @ 2020-02-14 06:52  CNoodle  阅读(429)  评论(0编辑  收藏  举报