Sunday, 20 December 2015

LeetCode Set Matrix Zeroes

LeetCode Set Matrix Zeroes

class Solution {

public:

    void setZeroes(vector<vector<int>>& matrix) {

        int m=matrix.size();

        if(m==0)

          return ;

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

        if(n==0)

          return ;

        vector < int > store_row;

        vector < int > store_col;

        vector < bool > visited_col(n,false);

        int i,j,tag=0;

        for(i=0;i<m;i++){

         tag=0;

         for(j=0;j<n;j++){

            if(matrix[i][j]==0 && tag==0)

              {

                  store_row.push_back(i);

                 store_col.push_back(j);

                  visited_col[j]=true;

                  tag=1;

              }

            if(matrix[i][j]==0 && visited_col[j]==false){

                   store_col.push_back(j);

                   visited_col[j]=true;

            }

         }

        }

        i=0;

        while(i<store_row.size()){

            for(j=0;j<n;j++){

               matrix[store_row[i]][j]=0;

            }

            i++;

        }

        i=0;

        while(i<store_col.size()){

            for(j=0;j<m;j++){

               matrix[j][store_col[i]]=0;

            }

            i++;

        }

    }

};

No comments:

Post a Comment