Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- http://leetcode.com/onlinejudge#question_73
- Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
- */
- class Solution {
- public:
- void setZeroes(vector<vector<int> > &matrix) {
- set<int> zerorows, zerocols;
- const int rows = matrix.size();
- const int cols = matrix[0].size();
- for(int i = 0; i < rows;++i)
- {
- for(int j = 0; j < cols; ++j)
- {
- if(matrix[i][j] == 0)
- {
- zerorows.insert(i);
- zerocols.insert(j);
- }
- }
- }
- for(set<int>::iterator it = zerorows.begin(); it != zerorows.end(); ++it)
- {
- for(int i = 0; i < cols; ++i)
- {
- matrix[*it][i] = 0;
- }
- }
- for(set<int>::iterator it = zerocols.begin(); it != zerocols.end(); ++it)
- {
- for(int i = 0; i < rows; ++i)
- {
- matrix[i][*it] = 0;
- }
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment