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;
            }
            
        }
    }
  • 相关阅读:
    乒乓球运动中两种最基本的握拍方法
    Google 的 OKR 制度与KPI 有什么不同?
    推荐物品时,为了消除个人特殊癖好,或者未打分的情况,可通过加权计算进行修正
    甘特图
    解耦、异步、削峰 消息队列
    供给侧
    货币化 经济货币化 信用 经济金融化
    新浪广告交易平台(SAX)DSP手册
    SU suspike命令学习
    SU Demos-02Filtering-02Subfilt
  • 原文地址:https://www.cnblogs.com/airwindow/p/4204870.html
Copyright © 2011-2022 走看看