【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),即利用矩阵本身进行存储,借用矩阵第一行与第一列进行记录。

代码一:空间复杂度O(m + n)

class Solution {
public:
    void setZeroes(vector<vector<int> > &matrix) {
        int m = matrix.size();
        if(m == 0)  return;
        int n = matrix[0].size();
        if(n == 0)  return;

        int row[m];
        int column[n];
        fill_n(row, m, 1);
        fill_n(column, n, 1);

        for(int i = 0; i < m; i++)
            for(int j = 0; j < n; j++)
                if(matrix[i][j] == 0)
                {
                    row[i] = 0;
                    column[j] = 0;
                }

        for(int i = 0; i < m; i++)
            for(int j = 0; j < n; j++)
                matrix[i][j] = (row[i] == 0 || column[j] == 0) ? 0 : matrix[i][j];

        return;
    }
};

代码二:空间复杂度O(1)

class Solution {
public:
    void setZeroes(vector<vector<int> > &matrix) {
        int m = matrix.size();
        if(m == 0)  return;
        int n = matrix[0].size();
        if(n == 0)  return;

        // 判断第一行与第一列是否存在0
        bool rowZero = false;
        bool columnZero = false;
        for(int i = 0; i < m; i++)
            if(matrix[i][0] == 0)
            {
                columnZero = true;
                break;
            }
        for(int i = 0; i < n; i++)
            if(matrix[0][i] == 0)
            {
                rowZero = true;
                break;
            }

        for(int i = 1; i < m; i++)
            for(int j = 1; j < n; j++)
                if(matrix[i][j] == 0)
                {
                    matrix[i][0] = 0;
                    matrix[0][j] = 0;
                }

        for(int i = 1; i < m; i++)
            for(int j = 1; j < n; j++)
                matrix[i][j] = (matrix[i][0] == 0 || matrix[0][j] == 0) ? 0 : matrix[i][j];

        if(rowZero == true)
            for(int i = 0; i < n; i++)
                matrix[0][i] = 0;
        if(columnZero == true)
            for(int i = 0; i < m; i++)
                matrix[i][0] = 0;

        return;
    }
};

【Leetcode】Set Matrix Zeroes

时间: 2024-10-03 13:40:11

【Leetcode】Set Matrix Zeroes的相关文章

【LeetCode】Set Matrix Zeroes 解题报告

今天看到CSDN博客的勋章换了图表,同时也增加显示了博客等级,看起来都听清新的,感觉不错! [题目] 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) spac

【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】Set Matrix Zeroes(middle)

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 思路:不能用额外空间,就用矩阵的第一行和第一列来标记这一行或这一列是否需要置0. 用两个bool量记录第一行和第一列是否需要置0 大神的代码和我的代码都是这个思路,但是我在画0的时候是行列分开处理的,大神的代码是一起处理的 void setZeroes(vector<vector<int> > &

【LeetCode】Spiral Matrix II

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ]此题与Spiral Matrix类似,可以用相同的方法解决,相比之下,此题比前一题简单 publ

【leetcode】Spiral Matrix

Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example,Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return [1,2,3,6,9,8,7,4,5]. 如果下一步会遇到访问

【leetcode】Spiral Matrix(middle)

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example,Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return [1,2,3,6,9,8,7,4,5]. 思路:就按照螺旋矩阵的规律 用n记录旋转圈数 每

【LeetCode】Factorial Trailing Zeroes

n久不做题了 ,之前因为考研,然后又是假期,一直懒得做,今天开始吧 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. 开始没有看到是阶乘,之后又研究复杂度的问题 代码如下: class Solution { public:     int trailingZeroes(int n) {      

【leetcode】Set Matrix Zeros

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】Factorial Trailing Zeroes (2 solutions)

Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. Credits:Special thanks to @ts for adding this problem and creating all test cases. 对n!做质因数分解n!=2x*