zoukankan      html  css  js  c++  java
  • [LeetCode#73]Set Matrix Zeroes

    The problem:

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

    My analysis:

    This is a very typical quesition in metricing our understanding about matrix.
    The main idea is that :
    We could not base on the changed columns or rows(have already been set to 0), to make our decision whether to reset a row or column. We must first scan the matrix, and then record the information(rows and columns needed to be reset) separately.
    Thus, we could have 3 options:
    1. use a flag matrix, which is the size of m * n.
    2. use a flag array for each row, and a flag array for each column, which is is the size of m + n.
    3. use two variable to store first row and first column's indicator respectively. Then, use first row and first column as indicator array for rows(2..n), columns(2..n). O(1) .
    Note: the first row's information would not be lost, since if there is a zero in the column (include the column in the first row), the elemet of the first row would be soonerly zeroed.

    The solution 3 is the most elegant and concise one. But when we scan the matrix, and reset the matrix, we shuld be very clear about the start point.
    1. For scan. We should start from first row and first column, cause the first row also have the information about which row needs to be zeroed, not just indicate the column should be zeroed (which we have already done).
    2. For reset. We should start form second row and first column, cause we have already use first row and first column for indicator purpose.

    My solution:

    public class Solution {
        public void setZeroes(int[][] matrix) {
            if (matrix.length == 0)
                return; 
            
            boolean first_row_zero = false; //used to record if the first row needed to be set to zero. 
            boolean first_column_zero = false;
            
            for (int i = 0; i < matrix.length; i++) {
                if (matrix[i][0] == 0)
                    first_column_zero = true;
            }
            
            for (int j = 0; j < matrix[0].length; j++) {
                if (matrix[0][j] == 0)
                    first_row_zero = true;
            }
            
            for (int i = 0; i < matrix.length; i++) {
                for (int j = 0; j < matrix[0].length; j++) {
                    if (matrix[i][j] == 0) {
                        matrix[i][0] = 0;
                        matrix[0][j] = 0;
                    }
                }
            }
            
            for (int i = 1; i < matrix.length; i++) { 
                //note: must start from second row and second column, Do not change first row or column. 
                if (matrix[i][0] == 0) { 
                    for (int j = 1; j < matrix[0].length; j++)
                        matrix[i][j] = 0;
                }
            }
            
            for (int j = 1; j < matrix[0].length; j++) {
                if (matrix[0][j] == 0) {
                    for (int i = 1; i < matrix.length; i++)
                        matrix[i][j] = 0;
                }
            }
            
            if (first_row_zero == true ) {
                for (int j = 0;  j < matrix[0].length; j++)
                    matrix[0][j] = 0;
            }
            
            if (first_column_zero == true) {
                for (int i = 0; i < matrix.length; i++)
                    matrix[i][0] = 0;
            }
            
        }
    }
  • 相关阅读:
    如何在 Microsoft Visual C# .NET 中实现 Microsoft Excel 自动化
    CentOS 6.5静态IP的设置(NAT和桥接都适用)
    myeclipse里如何添加mysql数据库
    Attribute value is quoted with " which must be escaped when used within the value 问题解决
    CentOS 6.5安装之后的网络配置
    Apache server for win解压版的安装与配置
    Oracle SQL 基本操作之 用户权限管理方法
    CentOS 6.5的安装详解
    IO类01
    可见性的问题
  • 原文地址:https://www.cnblogs.com/airwindow/p/4204870.html
Copyright © 2011-2022 走看看