-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsetMatrixZeroes.java
More file actions
106 lines (81 loc) · 2.15 KB
/
setMatrixZeroes.java
File metadata and controls
106 lines (81 loc) · 2.15 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*Given a m x n matrix, if an element is 0, set its entire row and column to 0.
Do it in-place.
Input format
First line contains 2 integers m,n - number of rows and columns respectively.
Next m lines contain space separated n integers.
Output format
Output the same matrix.
Sample Input 1
3 3
1 1 1
1 0 1
1 1 1
Sample Output 1
1 0 1
0 0 0
1 0 1
Explanation 1
There is one ‘0’ in 2nd row and 2nd column, so all the elements in that row and column become 0.
Sample Input 2
3 4
0 1 2 0
3 4 5 2
1 3 1 5
Sample Output 2
0 0 0 0
0 4 5 0
0 3 1 0
Explanation 2
1st row, 1st column and 4th column all contains ‘0’, so all the values in these rows & columns becomes 0.
Constraints
M,N <= 500
*/
import java.io.*;
import java.util.*;
class SetMatrixZeroes {
public void setMatrixZeroes(int[][] matrix) {
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
int[][] matrix = new int[n][m];
for(int i = 0 ; i < n ; ++i) {
for(int j = 0 ; j < m ; ++j) {
matrix[i][j] = in.nextInt();
}
}
in.close();
new SetMatrixZeroes().setMatrixZeroes(matrix);
List<Integer> rowVal = new ArrayList<Integer>();
List<Integer> colVal = new ArrayList<Integer>();
for(int i = 0 ; i < n ; ++i) {
for(int j = 0 ; j < m ; ++j) {
if(matrix[i][j] ==0){
rowVal.add(i);
colVal.add(j);
}
}
}
for(int i=0; i<rowVal.size(); i++){
int index=0;
while(m >index){
matrix[rowVal.get(i)][index] = 0;
index++;
}
}
for(int i=0; i<colVal.size(); i++){
int index=0;
while(n >index){
matrix[index][colVal.get(i)] = 0;
index++;
}
}
for(int i = 0 ; i < n ; ++i) {
for(int j = 0 ; j < m ; ++j) {
System.out.print(matrix[i][j] +" ");
}
System.out.println();
}
}
}