LeetCode OJ 之 Set Matrix Zeroes (矩阵的置0)

来源:互联网 发布:卖家网wish数据 编辑:程序博客网 时间:2024/06/15 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 x n 矩阵,如果存在一个元素为0,则把此元素所在的行列元素都置为0.

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)空间复杂度,可以设定两个bool型数组存储行和列上是否有0。

O(1)空间复杂度,可以利用二维数组的第一行和第一列。

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

class Solution {public:    void setZeroes(vector<vector<int> > &matrix)     {        if(matrix.empty())            return ;        int row = matrix.size();        int col = matrix[0].size();        vector<bool> vecRow(row,false);        vector<bool> vecCol(col,false);        for(int i = 0 ; i < row ; i++)        {            for(int j = 0 ; j < col ; j++)            {                if(matrix[i][j] == 0)                {                    vecRow[i] = true;                    vecCol[j] = true;                }            }        }        for(int i = 0 ; i < row ; i++)        {            if(vecRow[i])                for(int j = 0 ; j < col ; j++)                {                    matrix[i][j] = 0;                }        }        for(int i = 0 ; i < col ; i++)        {            if(vecCol[i])                for(int j = 0 ; j < row ; j++)                {                    matrix[j][i] = 0;                }        }        return;    }};

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

class Solution {public:    void setZeroes(vector<vector<int> > &matrix)     {        if(matrix.empty())            return ;        int row = matrix.size();        int col = matrix[0].size();        bool rowHasZero = false;        bool colHasZero = false;        //判断第一列是否有0,如果有,则最后把第一列都置为0        for(int i = 0 ; i < row ; i++)        {            if(matrix[i][0] == 0)            {                colHasZero = true;                break;            }        }        //判断第一行是否有0,如果有,则最后把第一行都置为0        for(int i = 0 ; i < col ; i++)        {            if(matrix[0][i] == 0)            {                rowHasZero = 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(rowHasZero)        {            for(int i = 0 ; i < col ; i++)            {                matrix[0][i] = 0;            }        }        if(colHasZero)        {            for(int i = 0 ; i < row ; i++)            {                matrix[i][0] = 0;            }        }        return ;    }};


0 0
原创粉丝点击