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 Kubernetes设计文档之服务篇-转
    基于kubernetes构建Docker集群管理详解-转
    Pass云Docker介绍
    Spring <context:annotation-config/> 解说
    webapp开发需要注意的浏览器内核知识
    koala编译scss文件时不支持中文字体的解决方案
    CSS3硬件加速需要注意的事项
    ios客户端快速滚动和回弹效果的实现
    mui禁止滚动条和禁止滚动
    苹果端禁用左右滑动屏幕返回上级页面
  • 原文地址:https://www.cnblogs.com/airwindow/p/4204870.html
Copyright © 2011-2022 走看看