leetcode笔记:Set Matrix Zeroes

一.题目描述

Given a m*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 not the best solution.

Could you devise a constant space solution?

二.题目分析

该题目最直观的解法就是开辟一个新的矩阵,当原矩阵存在零元素的时候,就将新矩阵的对应行和列置为零。这样空间复杂度较高,也是题目不允许的。

若要做到空间复杂度为常数,我的做法是就是利用矩阵的第一行和第一列来作为标记使用,这样便不用开辟新的存储空间。具体方法:

  1. 先确定第一行和第一列是否需要清零,即:遍历第一行中是否有0,也同时记下第一列中有没有0。在以下代码中,使用bool型变量x_keyy_key分别记录第一行和第一列的情况;
  2. 扫描剩下的矩阵元素,如果遇到了0,就将该元素所对应的第一行和第一列上的元素赋值为0
  3. 在遍历完二维数组后,就可以根据第一行和第一列的信息,将剩下的矩阵元素进行赋值。拿第一行为例,如果扫描到第i个元素为0,就将二维数组的第i列全部置0
  4. 最后,根据1中bool型变量x_keyy_key的值,处理第一行和第一列。如果最开始得到的第一行中有0的话,就整行清零,对第一列也采取同样的处理。

三.示例代码

第一种方法如下:

#include <vector>
using namespace std;

class Solution
{
public:
    // 时间复杂度O(m * n),空间复杂度O(m + n)
    void setZeros(vector<vector<int> >& matrix)
    {
        const size_t x = matrix.size();
        const size_t y = matrix[0].size();
        if (x == 0 || y == 0) return;
        vector<bool> rowRes(x, false);
        vector<bool> colRes(y, false);

        for (size_t i = 0; i < x; i++)
        {
            for (size_t j = 0; j < y; j++)
            {
                if (matrix[i][j] == 0)
                    rowRes[i] = colRes[j] = true;
            }
        }

        // set zero
        for (size_t i = 0; i < x; i++)
        {
            if (rowRes[i])
                for (size_t k = 0; k < x; k++)
                    matrix[i][k] = 0;
        }
        for (size_t j = 0; j < y; j++)
        {
            if (colRes[j])
                for (size_t k = 0; k < x; k++)
                    matrix[k][j] = 0;
        }
    }
};

以上方法的空间复杂度为O(m + n),并不能达到题目要求的最终要求。

第二种方法如下:

#include <vector>
using namespace std;

class Solution
{
public:
    void setZerosBetter(vector<vector<int> >& matrix)
    {
        const size_t x = matrix.size();
        const size_t y = matrix[0].size();
        bool x_key = false, y_key = false;
        if (x == 0 || y == 0) return;

        for (size_t i = 0; i < y; i++)
        {
            if (matrix[0][i] == 0)
            {
                x_key = true;
                break;
            }
        }

        for (size_t i = 0; i < x; i++)
        {
            if (matrix[i][0] == 0)
            {
                y_key = true;
                break;
            }
        }

        for (size_t i = 0; i < x; i++)
        {
            for (size_t j = 0; j < y; j++)
            {
                if (matrix[i][j] == 0 && i > 0 && j > 0)
                {
                    matrix[i][0] = 0;
                    matrix[0][j] = 0;
                }
            }
        }
        // 调整1~x行、1~y列的元素
        for (size_t i = 1; i < x; i++)
            if (matrix[i][0] == 0)
            {
                for (size_t k = 1; k < y; k++)
                    matrix[i][k] = 0;
            }
        for (size_t j = 1; j < y; j++)
            if (matrix[0][j] == 0)
            {
                for (size_t k = 1; k < x; k++)
                    matrix[k][j] = 0;
            }

        // 最后调整第一行第一列

        if (y_key)
            for (size_t k = 0; k < x; k++)
                matrix[k][0] = 0;

        if (x_key)
            for (size_t k = 0; k < y; k++)
                matrix[0][k] = 0;
    }
};

四.小结

这道题如果只是仅仅想实现功能的话,不需要什么技巧,只有提高对空间复杂度的要求才能体现出算法设计的思想。

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2025-01-09 05:56:39

leetcode笔记:Set Matrix Zeroes的相关文章

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 使用第一行和第一列记录某

【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 (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#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 cha

leetcode 【 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. 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 us

leetcode No73. Set Matrix Zeroes

Question: Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 矩阵中如果有0,则该行该列都置0 Algorithm: 遍历矩阵,用两个数组记录0元素的行下标和列下标,再将数组记录的行和列置0 Accepted Code: class Solution { public: void setZeroes(vector<vector<int>>

[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

Java for LeetCode 073 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. 解题思路: 用两个boolean数组row col表示行列是否有零即可,JAVA实现如下: public void setZeroes(int[][] matrix) { if (matrix.length == 0 || matrix[0].length == 0) return; boolean[] row