Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
解题思路:题目很简单,可就是没想到有什么高效的方法.我遍历了两遍vector,显得有点复杂.
#include<iostream> #include<vector> using namespace std; void setZeroes(vector<vector<int> > &matrix) { if (matrix.empty()) return; vector<bool>BeZero_Row(matrix.size(), false); vector<bool>BeZero_Cloume(matrix[0].size(), false); for (int i = 0; i != matrix.size();++i){ if (BeZero_Row[i]) continue; for (int j = 0; j != matrix[i].size();++j){ if (matrix[i][j]==0){ BeZero_Cloume[j] = true; BeZero_Row[i] = true; } } } for (int i = 0; i != matrix.size(); ++i){ for (int j = 0; j != matrix[i].size(); ++j) { if (BeZero_Cloume[j]||BeZero_Row[i]){ matrix[i][j] = 0; } } } }
原文:http://blog.csdn.net/li_chihang/article/details/43456819