runnig

[leetcode] 73 Set Matrix Zeroes

Feb 5th, 2013
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. /*
  2.  
  3. http://leetcode.com/onlinejudge#question_73
  4.  
  5. Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
  6.  
  7. */
  8. class Solution {
  9. public:
  10.     void setZeroes(vector<vector<int> > &matrix) {
  11.         set<int> zerorows, zerocols;
  12.        
  13.         const int rows = matrix.size();
  14.         const int cols = matrix[0].size();
  15.        
  16.         for(int i = 0; i < rows;++i)
  17.         {
  18.             for(int j = 0; j < cols; ++j)
  19.             {
  20.                 if(matrix[i][j] == 0)
  21.                 {
  22.                     zerorows.insert(i);
  23.                     zerocols.insert(j);
  24.                 }
  25.             }
  26.         }
  27.        
  28.         for(set<int>::iterator it = zerorows.begin(); it != zerorows.end(); ++it)
  29.         {
  30.             for(int i = 0; i < cols; ++i)
  31.             {
  32.                 matrix[*it][i] = 0;
  33.             }
  34.         }
  35.         for(set<int>::iterator it = zerocols.begin(); it != zerocols.end(); ++it)
  36.         {
  37.             for(int i = 0; i < rows; ++i)
  38.             {
  39.                 matrix[i][*it] = 0;
  40.             }
  41.         }
  42.     }
  43. };
Advertisement
Add Comment
Please, Sign In to add comment