Find an algorithm that contains 0 in a two-dimensional matrix and converts all the rows and columns containing 0 to 0!

Source: Internet
Author: User

Import java. util. arraylist;

Import java. util. List;

 

Public class matrixdemo {

 

// Find all sets of zero elements in the two-dimensional matrix

Public static list <posiztion> findzero (int A [] []) {

List <posiztion> List = new arraylist <posiztion> ();

Int ROW = A. length;

Int Col = A [0]. length;

For (INT I = 0; I <row; I ++ ){

For (Int J = 0; j <Col; j ++ ){

If (A [I] [J] = 0 ){

Posiztion P = new posiztion ();

P. setx (I );

P. sety (j );

List. Add (P );

}

}

}

Return list;

}

 

// Replace all rows and columns of the element 0 in the matrix with 0

Public static int [] [] replacezero (int A [] []) {

Int B [] [] =;

List <posiztion> L = findzero ();

For (INT I = 0; I <L. Size (); I ++ ){

Posiztion P = L. Get (I );

Int x = P. getx ();

Int y = P. Gety ();

 

For (Int J = 0; j <A. length; j ++ ){

B [J] [Y] = 0;

}

 

For (int K = 0; k <A [0]. length; k ++ ){

B [x] [k] = 0;

}

}

Return B;

}

 

// Print the functions of the two-dimensional matrix

Public static void printmatrix (int A [] []) {

For (INT I = 0; I <A. length; I ++ ){

For (Int J = 0; j <A [0]. length; j ++ ){

System. Out. Print (A [I] [J] + "");

}

System. Out. println ();

}

}

 

Public static void main (string [] ARGs ){

Int A [] [] = {1, 2, 3, 4, 5}, {1, 2, 3, 4, 0}, {0, 1, 2, 3, 4}, {1, 2, 3, 4, 5 }};

List <posiztion> L = findzero ();

System. Out. println ("the position of zero are :");

For (INT I = 0; I <L. Size (); I ++ ){

System. Out. println ("(" + L. Get (I). getx () + "," + L. Get (I). Gety ()

+ ")");

}

System. Out. println ("before Replace the matrix is :");

Printmatrix ();

System. Out. println ("after replace the matrix is :");

Int B [] [] = replacezero ();

Printmatrix (B );

}

 

}

 

// Define a class to store the position of the 0 element. X indicates the row in which the column is located and Y indicates the column in which the element is located.

Class posiztion {

Private int X;

Private int y;

 

Public int getx (){

Return X;

}

 

Public void setx (int x ){

This. x = X;

}

 

Public int Gety (){

Return y;

}

 

Public void sety (INT y ){

This. Y = y;

}

 

}

 

Running result:

 

The position of zero are:

(1, 4)

(2, 0)

Before Replace the matrix is:

1 2 3 4 5

1 2 3 4 0

0 1 2 3 4

1 2 3 4 5

After Replace the matrix is:

0 2 3 4 0

0 0 0 0 0

0 0 0 0 0

0 2 3 4 0

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.