[程序员面试宝典]清除行列

来源:互联网 发布:琉璃神社备用域名 编辑:程序博客网 时间:2024/05/17 04:40


思路:

设置一个行的矩阵,和列的矩阵,保存元素为0的行号和列号

如果行号或者列号被标记,赋值为0


class Clearer {public:    vector<vector<int> > clearZero(vector<vector<int> > mat, int n) {        int row[n];  //hang        int colum[n]; //lie                for(int i=0; i<n; i++)            row[i] = colum[i] = 0;                for(int i=0; i<n; i++)            for(int j=0; j<n; j++)                {                if(mat[i][j]==0)                    {                    row[i] = 1;                    colum[j] = 1;                }            }                for(int i=0; i<n; i++)            for(int j=0; j<n; j++)                {                if( (row[i]==1)||(colum[j]==1) )                    mat[i][j]=0;            }                return mat;    }};



0 0