找到一个二维矩阵中所有包含0的,并且把0元素所在行与列全部转换成0的算法!

 

  
  
  
  
  1. import java.util.ArrayList;  
  2.  
  3. import java.util.List;  
  4.  
  5. public class MatrixDemo {  
  6.  
  7. // 找出二维矩阵中为0元素的所有集合  
  8.  
  9. public static List<Posiztion> findZero(int a[][]) {  
  10.  
  11. List<Posiztion> list = new ArrayList<Posiztion>();  
  12.  
  13. int row = a.length;  
  14.  
  15. int col = a[0].length;  
  16.  
  17. for (int i = 0; i < row; i++) {  
  18.  
  19. for (int j = 0; j < col; j++) {  
  20.  
  21. if (a[i][j] == 0) {  
  22.  
  23. Posiztion p = new Posiztion();  
  24.  
  25. p.setX(i);  
  26.  
  27. p.setY(j);  
  28.  
  29. list.add(p);  
  30.  
  31. }  
  32.  
  33. }  
  34.  
  35. }  
  36.  
  37. return list;  
  38.  
  39. }  
  40.  
  41. // 将矩阵中的为0的元素的行与列全部替换为0  
  42.  
  43. public static int[][] replaceZero(int a[][]) {  
  44.  
  45. int b[][] = a;  
  46.  
  47. List<Posiztion> l = findZero(a);  
  48.  
  49. for (int i = 0; i < l.size(); i++) {  
  50.  
  51. Posiztion p = l.get(i);  
  52.  
  53. int x = p.getX();  
  54.  
  55. int y = p.getY();  
  56.  
  57. for (int j = 0; j < a.length; j++) {  
  58.  
  59. b[j][y] = 0;  
  60.  
  61. }  
  62.  
  63. for (int k = 0; k < a[0].length; k++) {  
  64.  
  65. b[x][k] = 0;  
  66.  
  67. }  
  68.  
  69. }  
  70.  
  71. return b;  
  72.  
  73. }  
  74.  
  75. // 打印二维矩阵的函数  
  76.  
  77. public static void printMatrix(int a[][]) {  
  78.  
  79. for (int i = 0; i < a.length; i++) {  
  80.  
  81. for (int j = 0; j < a[0].length; j++) {  
  82.  
  83. System.out.print(a[i][j] + " ");  
  84.  
  85. }  
  86.  
  87. System.out.println();  
  88.  
  89. }  
  90.  
  91. }  
  92.  
  93. public static void main(String[] args) {  
  94.  
  95. int a[][] = { { 12345 }, { 12340 }, { 01234 } ,{1,2,3,4,5}};  
  96.  
  97. List<Posiztion> l = findZero(a);  
  98.  
  99. System.out.println("The position of zero are:");  
  100.  
  101. for (int i = 0; i < l.size(); i++) {  
  102.  
  103. System.out.println("(" + l.get(i).getX() + "," + l.get(i).getY()  
  104.  
  105. ")");  
  106.  
  107. }  
  108.  
  109. System.out.println("Before replace the Matrix is:");  
  110.  
  111. printMatrix(a);  
  112.  
  113. System.out.println("After replace the Matrix is:");  
  114.  
  115. int b[][] = replaceZero(a);  
  116.  
  117. printMatrix(b);  
  118.  
  119. }  
  120.  
  121. }  
  122.  
  123. // 定义一个类,用来存放为0元素的位置,x代表在哪行,y代表在哪列  
  124.  
  125. class Posiztion {  
  126.  
  127. private int x;  
  128.  
  129. private int y;  
  130.  
  131. public int getX() {  
  132.  
  133. return x;  
  134.  
  135. }  
  136.  
  137. public void setX(int x) {  
  138.  
  139. this.x = x;  
  140.  
  141. }  
  142.  
  143. public int getY() {  
  144.  
  145. return y;  
  146.  
  147. }  
  148.  
  149. public void setY(int y) {  
  150.  
  151. this.y = y;  
  152.  
  153. }  
  154.  
  155. }  
  156.  
  157.  

 

运行结果:


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

 

 

你可能感兴趣的:(算法,职场,元素,矩阵,休闲)