[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;
        }

    }
}
时间: 2024-08-09 23:47:12

[LeetCode#73]Set Matrix Zeroes的相关文章

[LeetCode] 73. Set Matrix Zeroes 解题思路

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Follow up: Did you use extra space?A straight forward solution using O(mn) space is probably a bad idea.A simple improvement uses O(m + n) space, but still

leetcode 73 Set Matrix Zeroes ----- java

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click to show follow up. Follow up: Did you use extra space?A straight forward solution using O(mn) space is probably a bad idea.A simple improvement uses O

LeetCode:Set Matrix Zeroes - 将矩阵内含有零的行和列全部置零

1.题目名称 Set Matrix Zeroes(将矩阵内含有零的行和列全部置零) 2.题目地址 https://leetcode.com/problems/set-matrix-zeroes/ 3.题目内容 英文:Given a m x n matrix, if an element is 0, set its entire row and column to 0. 中文:给定一个m×n的矩阵,如果其中一个元素是0,则将该元素所在的整行或整理全部置为0 4.解题方法1 使用第一行和第一列记录某

73. Set Matrix Zeroes &amp;&amp; 289. Game of Life

73. Set Matrix Zeroes Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Hide Tags Array Hide Similar Problems (M) Game of Life public class Solution { //Only consider the zeros that exist originally. public

【Leetcode】Set Matrix Zeroes

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Could you devise a constant space solution? 思路:因为需要遍历整个矩阵,时间复杂度肯定需要O(m * n),对于空间复杂度而言,第一种是可以使用O(m * n),对每个位置的0的情况进行记录,第二种是使用O(m + n),对每行每列是否存在0进行记录,第三种是O(1)

73. Set Matrix Zeroes(js)

73. Set Matrix Zeroes Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place. Example 1: Input: [   [1,1,1],   [1,0,1],   [1,1,1] ] Output: [   [1,0,1],   [0,0,0],   [1,0,1] ] Example 2: Input: [   [0,1,2,0],   [

【LeetCode】Set Matrix Zeroes (2 solutions)

Set Matrix Zeroes Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click to show follow up. Follow up: Did you use extra space?A straight forward solution using O(mn) space is probably a bad idea.A simple

【一天一道LeetCode】#73. Set Matrix Zeroes

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. (二)解题 题目大意:给定一个M*N的数组,如果(i,j)为0,则将第i行第j列全部元素置为0. 这道题目意思很简单,如果考虑

【LeetCode】-- 73. Set Matrix Zeroes

问题描述:将二维数组中值为0的元素,所在行或者列全set为0:https://leetcode.com/problems/set-matrix-zeroes/ 问题分析:题中要求用 constant space 的辅助空间.自然想到位优化.一个int可以存储31个元素的信息.这里刚好用到了字符串论文里面常用的优化处理方法.类似桶分的思想.好吧,这么看来这长时间的论文没白看. 附上代码: 1 void setZeroes(vector<vector<int>>& matrix