1.7 array-Clear rows and columns

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.