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;
            }
            
        }
    }
  • 相关阅读:
    Python __repr__()方法:显示属性
    Python SQLAlchemy入门教程(基本用法)
    彻底搞懂Token、Session和Cookie。
    MTV和MVC的区别
    Flask配置Cors跨域
    跨域资源共享 CORS 详解
    浏览器同源政策及其规避方法
    敏捷开发
    Nginx搭建正向代理服务器支持https
    为什么使用k8s和容器作为devops的底层平台
  • 原文地址:https://www.cnblogs.com/airwindow/p/4204870.html
Copyright © 2011-2022 走看看