Title Description
Write an algorithm that, if an element in the MXN matrix is 0, it will be in the same row as Lieqing zero.
Given a int[][] matrix of MXN (vector<vector> in C + +)Mat and the order of the matrix N, return to the completed operation of the int[][] matrix ( Vector<vector> in C + +), guaranteeing that n is less than or equal to 300, the elements in the matrix are in the int range.
Test examples:
[[1,2,3],[0,1,2],[0,0,1]]
return: [[0,0,3],[0,0,0],[0,0,0]]
Solution: Loop search, statistics record 0 of the row and column coordinates, and then to clear 0 operation, with two flags to mark, rows and columns appear 0
#include <iostream> #include <vector>using namespace std;void printmat (vector<vector<int> > Mat, int n) {for (Int. i=0; i < n; i++) {for (int j = 0; J < N; j + +) cout << Mat[i][j] &l t;< ""; cout << Endl; }}vector<vector<int> > Clearzero (vector<vector<int> > Mat, int n) {//write code here Loop search, statistics record 0 in the row and column coordinates, and then to clear 0 operation Vector<int> Rol (n), col (n); for (int i = 0; i < n; i++) {for (int j = 0; J &L T N J + +) {if (mat[i][j] = = 0) {Col[j] = 1;rol[i] = 1;}}} for (int i = 0, i < n; i++) {for (int j = 0; J < N; j + +) {if (rol[i] = = 1 | | col[j] = = 1) mat[i][j] = 0; }}printmat (Mat, n); return mat; }int Main (int argc, char** argv) {int n = 5; vector<vector<int> > Array (n); 3 Vectors for (int i = 0; i < n; i++) {array[i].resize (n);//sets the size of the array 3x3} for (int i = 0; I < n; i++) {for (int j = 0; j < N;J + +) {Array[i][j] = (n * i-5); }} printmat (array, n); Clearzero (array, n); return 0;}
1.7 array-Clear rows and columns