【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 improvement uses O(m + n) space, but still not the best solution.
Could you devise a constant space solution?

解法一:

使用数组分别记录需要置零的行列。然后根据数组信息对相应行列置零。

空间复杂度O(m+n)

class Solution {
public:
    void setZeroes(vector<vector<int> > &matrix) {
        if(matrix.empty() || matrix[0].empty())
            return;

        int m = matrix.size();
        int n = matrix[0].size();

        vector<bool> row(m, false);
        vector<bool> col(n, false);

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

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

解法二:

使用第一行和第一列记录该行和该列是否应该置零。

对于由此覆盖掉的原本信息,只要单独遍历第一行第一列判断是否需要置零即可。

空间复杂度O(1)

class Solution {
public:
    void setZeroes(vector<vector<int> > &matrix) {
        if(matrix.empty() || matrix[0].empty())
            return;

        int m = matrix.size();
        int n = matrix[0].size();

        bool firstRow = false;
        bool firstCol = false;

        for(int i = 0; i < m; i ++)
        {
            if(matrix[i][0] == 0)
            {
                firstCol = true;
                break;
            }
        }
        for(int j = 0; j < n; j ++)
        {
            if(matrix[0][j] == 0)
            {
                firstRow = 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;
                }
            }
        }
        //set zero
        for(int i = 1; i < m; i ++)
        {
            for(int j = 1; j < n; j ++)
            {
                if(matrix[i][0] == 0)
                    matrix[i][j] = 0;
                if(matrix[0][j] == 0)
                    matrix[i][j] = 0;
            }
        }
        if(firstRow == true)
        {
            for(int j = 0; j < n; j ++)
            {
                matrix[0][j] = 0;
            }
        }
        if(firstCol == true)
        {
            for(int i = 0; i < m; i ++)
            {
                matrix[i][0] = 0;
            }
        }
    }
};

时间: 2024-08-27 21:08:18

【LeetCode】Set Matrix Zeroes (2 solutions)的相关文章

【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)

【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(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】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*

【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】Search Insert Position (2 solutions)

Search Insert Position Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples.[1,3,5,

【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) {