您的位置:首页 > 其它

leetcode 073 Set Matrix Zeroes

2016-05-02 19:27 393 查看
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) {
int rows = matrix.size(), cols = 0;

if(rows == 0) return ;

cols = matrix[0].size();

set<int> rowNum;
set<int> colNum;

for(int i=0; i < rows; i++) {
for(int j=0; j < cols; j++) {
if(matrix[i][j] == 0) {
rowNum.insert(i);
colNum.insert(j);
}
}
}

set<int>::iterator itr;
for(itr=rowNum.begin(); itr!=rowNum.end(); itr++) {
int temp = *itr;

for(int i = 0; i < cols; i++)
matrix[temp][i] = 0;
}

for(itr=colNum.begin(); itr!=colNum.end(); itr++) {
int temp = *itr;

for(int i=0; i < rows; i++) {
matrix[i][temp] = 0;
}
}
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: