Middle-题目39:73. Set Matrix Zeroes

原创 2016年05月31日 16:01:21

题目原文:
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的矩阵,若一个元素是0,置它所在的整行和整列都为0.
题目分析:
时间复杂度是O(mn)无疑,本题考查的是空间复杂度。
方法一:O(m+n)的空间:使用两个数组isRowsZero和isColsZero分别记录每一行和每一列是否需要置0.
方法二:O(1)的空间:使用第一行和第一列记录该行和该列是否应该置零,并使用两个变量rol0和col0记录首行和首列是否需要置0(因为有可能把不是0的信息变成0)
源码:(language:java/c)
方法一:

public class Solution {
    public void setZeroes(int[][] matrix) {
        int rows=matrix.length;
        int cols=matrix[0].length;
        boolean[] isZeroRow = new boolean[rows];
        boolean[] isZeroCol = new boolean[cols];
        for(int i = 0;i <rows;i++) {
            for(int j = 0;j < cols;j++) {
                if(matrix[i][j]==0) {
                    isZeroRow[i]=true;
                    isZeroCol[j]=true;
                }
            }
        }
        for(int i = 0;i<rows;i++) {
            if(isZeroRow[i]) {
                for(int j=0;j<cols;j++) {
                    matrix[i][j]=0;
                }
            }
        }
        for(int j=0;j<cols;j++) {
            if(isZeroCol[j]) {
                for(int i=0;i<rows;i++) {
                    matrix[i][j]=0;
                }
            }
        }
    }
}

方法二:

void setZeroes(int** matrix, int matrixRowSize, int matrixColSize) {
        int col0 = 0;
        int row0 = 0;
        for(int i = 0; i < matrixRowSize; i++) {
            if(matrix[i][0] == 0) {
                col0 = 1;
                break;
            }
        }
        for(int j = 0; j < matrixColSize; j++) {
            if(matrix[0][j] == 0) {
                row0 = 1;
                break;
            }
        }
        for(int i = 1;i < matrixRowSize; i++) {
            for(int j = 1;j < matrixColSize; j++) {
                if(matrix[i][j] == 0) {
                    matrix[i][0] = 0;
                    matrix[0][j] = 0;
                }
            }
        }
        for(int i = 1;i < matrixRowSize;i++) {
            if(!matrix[i][0]) {
                for(int j = 1;j < matrixColSize;j++) {
                    matrix[i][j]=0;
                }
            }
        }
        for(int j = 1;j < matrixColSize;j++) {
            if(!matrix[0][j]) {
                for(int i = 1;i < matrixRowSize;i++) {
                    matrix[i][j]=0;
                }
            }
        }
        if(col0) {
            for(int i = 0; i < matrixRowSize; i++)
                matrix[i][0] = 0;
        }
        if(row0) {
            for(int j = 0; j < matrixColSize; j++)
                matrix[0][j] = 0;
        }
}

成绩:
方法一:2ms,beats 19.41%,众数2ms,19.41%
方法二:44ms,beats 21.43%, 众数44ms,69.05%
cmershen的碎碎念:
空间复杂度是leetcode上很多题的要求,应该也是面试时需要重点注意的一项。

版权声明:完整版Leetcode题解请出门左转https://github.com/cmershen1/leetcode/tree/master/docs

相关文章推荐

【LeetCode】73. Set Matrix Zeroes解法及注释

73. Set Matrix Zeroes Given a m x n matrix, if an element is 0, set its entire row and column to ...

[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. ...

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. ...

Leetcode NO.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 ...

(java)leetcode-73:Set Matrix Zeroes

Set Matrix Zeroes Given a m x n matrix, if an element is 0, set its entire row and column to 0. D...

Leetcode OJ 73 Set Matrix Zeroes [Medium]

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

Leetcode 73. Set Matrix Zeroes (Medium) (cpp)

Leetcode 73. Set Matrix Zeroes (Medium) (cpp)

leetcode_question_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. Foll...

LeetCode 73 Set Matrix Zeroes(Python详解及实现)

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

[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. 题意 ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)