zoukankan      html  css  js  c++  java
  • [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

    矩阵置零。

    题意是给一个二维矩阵,如果其中有任何一个元素为0,请将0所在的行和列全都赋值为0。要求in-place做。

    数组类型的题有好几道都明确要求不能使用额外空间,这题也不例外。因为不能用到额外空间,所以只能将是否需要赋零的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         for (int i = 0; i < m; i++) {
    14             for (int j = 0; j < n; j++) {
    15                 if (matrix[i][j] == 0) {
    16                     matrix[0][j] = 0;
    17                     matrix[i][0] = 0;
    18                     if (i == 0) {
    19                         row = true;
    20                     }
    21                     if (j == 0) {
    22                         col = true;
    23                     }
    24                 }
    25             }
    26         }
    27 
    28         // starting from the second row and second column
    29         for (int i = 1; i < m; i++) {
    30             if (matrix[i][0] == 0) {
    31                 for (int j = 1; j < n; j++) {
    32                     matrix[i][j] = 0;
    33                 }
    34             }
    35         }
    36         for (int j = 1; j < n; j++) {
    37             if (matrix[0][j] == 0) {
    38                 for (int i = 1; i < m; i++) {
    39                     matrix[i][j] = 0;
    40                 }
    41             }
    42         }
    43         if (row) {
    44             for (int j = 0; j < n; j++) {
    45                 matrix[0][j] = 0;
    46             }
    47         }
    48         if (col) {
    49             for (int i = 0; i < m; i++) {
    50                 matrix[i][0] = 0;
    51             }
    52         }
    53     }
    54 }

    相关题目

    73. Set Matrix Zeroes

    289. Game of Life

    LeetCode 题目总结

  • 相关阅读:
    Grails入门教程(二)
    为脚本语言平反JavaScript篇(2)
    【CEO来信】李开复:创新工场我的新公司
    api测试常用测试点
    04 jmeter线程介绍及脚本编写
    02 改变jmeter工具GUI永久使用中文方法
    Mac配置hosts文件
    03 GUI界面的错误日志查看及清除
    页面存在多个url,使用jmeter进行遍历操作
    2.0数据之独立存储(Isolated Storage)
  • 原文地址:https://www.cnblogs.com/cnoodle/p/12306174.html
Copyright © 2011-2022 走看看