Leetcode#73 Set Matrix Zeroes

原题地址

用矩形的第一行和第一列充当mask

代码:

 void setZeroes(vector<vector<int> > &matrix) {
if (matrix.empty() || matrix[].empty()) return; bool firstRow = false;
bool firstCol = false;
int m = matrix.size();
int n = matrix[].size(); for (int i = ; i < m; i++)
if (matrix[i][] == ) {
firstCol = true;
break;
} for (int j = ; j < n; j++)
if (matrix[][j] == ) {
firstRow = true;
break;
} for (int i = ; i < m; i++) {
for (int j = ; j < n; j++) {
if (matrix[i][j] == ) {
matrix[i][] = ;
matrix[][j] = ;
}
}
} for (int i = ; i < m; i++)
if (matrix[i][] == )
for (int j = ; j < n; j++)
matrix[i][j] = ; for (int j = ; j < n; j++)
if (matrix[][j] == )
for (int i = ; i < m; i++)
matrix[i][j] = ; if (firstCol)
for (int i = ; i < m; i++)
matrix[i][] = ;
if (firstRow)
for (int j = ; j < n; j++)
matrix[][j] = ;
}
上一篇:Latency


下一篇:leetcode[73] Set Matrix Zeroes 将矩阵置零