-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrixMagic.java
59 lines (51 loc) · 1.57 KB
/
MatrixMagic.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
Given a matrix, mark all the rows and columns of each 0 in the matrix
**/
import java.util.*;
class MatrixMagic {
public Stack getZeroPositions(int[][] input) {
Stack zeroPosStack = new Stack();
System.out.println("Matrix is: \n");
for (int row = 0; row < input.length; row++) {
for (int col = 0; col < input.length; col++) {
System.out.print(input[row][col] + " ");
if (input[row][col] == 0) {
zeroPosStack.push(row + "," + col);
}
}
System.out.println();
}
return zeroPosStack;
}
public void zeroAllRowsAndColsForZeroPos(int[][] input) {
Stack zeroPosStack = getZeroPositions(input);
while (zeroPosStack.size() != 0) {
String[] splitArray = zeroPosStack.pop().toString().split(",");
int row = Integer.parseInt(splitArray[0]);
int col = Integer.parseInt(splitArray[1]);
input = zeroRowColForGivenPos(input, row, col);
}
Stack ignoreStack = getZeroPositions(input);
}
private int[][] zeroRowColForGivenPos(int[][] input, int inputRow, int inputCol) {
// zero for a given row
for (int col = 0; col < input.length; col++) {
input[inputRow][col] = 0;
}
// zero for a given col
for (int row = 0; row < input.length; row++) {
input[row][inputCol] = 0;
}
return input;
}
public static void main(String args[]) {
MatrixMagic mm = new MatrixMagic();
int[][] input = {{1,1,0}, {0,1,1}, {1,1,1}};
Stack zeroPosStack = mm.getZeroPositions(input);
System.out.println("Positions of 0s in (row,col): \n");
while (zeroPosStack.size() != 0) {
System.out.println(zeroPosStack.pop());
}
mm.zeroAllRowsAndColsForZeroPos(input);
}
}