LeetCode—Set Matrix Zeroes 矩阵数组值为0,至行,列为0

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

题目没有什么难度,但是可以在空间复杂度上做一些处理:

开始写的算法比较简单,将行和列中为0的部分记录下来,然后再经过一个赋值操作:

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<int> row;
        vector<int> col;
        for(int i = 0; i < m; i++)
        {
            for(int j = 0; j < n; j++)
            {
                if(matrix[i][j] == 0)
                {
                    if(find(col.begin(),col.end(),j) == col.end())
                    {
                        col.push_back(j);
                    }
                    if(find(row.begin(),row.end(),i) == row.end())
                    {
                        row.push_back(i);
                    }
                }
            }
        }
        for(int i = 0; i < row.size(); i++)
        {
            for(int j = 0; j < n; j++)
            {
                matrix[row[i]][j] = 0;
            }
        }
        for(int i = 0; i < m; i++)
        {
            for(int j = 0; j <col.size(); j++)
            {
                 matrix[i][col[j]] = 0;
            }
        }
    }
};

还有一种比较好的做法就是利用已有的内存空间进行操作:

1 首先判断第一行,第一列是否需要置0

2 利用第一行,和第一列,记录当前位置如果为0,需要置0的行和列的位置,也就是利用第一行,第一列保存中间值

3 对应行,列置0

4 第一行,第一列进行处理:

void setZeroes(vector<vector<int> > &matrix) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        int row = matrix.size();
        if(row == 0) return;
        int col = matrix[0].size();
        if(col == 0) return;

        bool firstrowiszero = false;
        bool firstcoliszero = false;
        for(int j = 0; j < col; ++j)
            if(matrix[0][j] == 0){
                firstrowiszero = true;
                break;
            }
        for(int i = 0; i < row; ++i)
            if(matrix[i][0] == 0){
                firstcoliszero = true;
                break;
            }

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

        for(int i = 1; i < row; ++i)
            for(int j = 1; j < col; ++j)
                if(matrix[i][0] == 0 || matrix[0][j] == 0)
                    matrix[i][j] = 0;

        if(firstrowiszero){
            for(int j = 0; j < col; ++j)
                matrix[0][j] = 0;
        }
        if(firstcoliszero){
            for(int i = 0; i < row; ++i)
                matrix[i][0] = 0;
        }
    }

时间: 2024-08-15 04:06:00

LeetCode—Set Matrix Zeroes 矩阵数组值为0,至行,列为0的相关文章

[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. 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 矩阵置零

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(m n) space is probably a bad idea.A simple improvement uses

LeetCode: Set Matrix Zeroes [073]

[题目] Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. [题意] 给定一个mXn的矩阵,如果其中的元素为0,则对应的行和列都用0填充. 不能申请额外的空间. [思路] 第一行和第一列空出来标记需要置0的列和行 第一遍扫描: 扫描第一行,判断第一行是否需要清零 扫描第一列,判断第一列是否需要清零 扫描[1,1]~[m-1, n-1]的区块,如果某个位置上的值

[CareerCup] 1.7 Set Matrix Zeroes 矩阵赋零

1.7 Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column are set to 0. LeetCode中的原题,请参见我之前的博客Set Matrix Zeroes 矩阵赋零.

LeetCode:Move Zeroes - 将数组中的0移到最后

1.题目名称 Move Zeroes(将数组中的0移到最后) 2.题目地址 https://leetcode.com/problems/move-zeroes 3.题目内容 英文:Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements. 中文:给出一个数字数组,写一个函数将数组中所有的0移

LeetCode -- 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. Analysis: 给出一个m * n的矩阵,如果一个元素为0,则它整行整列都设为0. Answer: public class Solution { public void setZeroes(int[][] matrix) { ArrayList<Integer> colll = ne

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. 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 (矩阵置零)

给定一个矩阵,如果有零元素那么就将零元素所在的行和列都置为零. Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 最直观的解法就是开辟一个新的矩阵,当原矩阵存在零元素的时候,就将新矩阵的对应行和列置为零.这样空间复杂度较高,也是题目不允许的. 题目的难点就在于,如果遇到零元素之后马上在矩阵上操作,将所在的行和列都置为零.在接下来的遍历中,如果你再遇到零,你讲不

[Leetcode] 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]. 题意:以螺旋的方式,顺时针访问数组. 思路:按照遍