forked from dharmanshu1921/porject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsam_make_all_rows_cols_0_using_extra_space.java
72 lines (61 loc) · 1.33 KB
/
sam_make_all_rows_cols_0_using_extra_space.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
60
61
62
63
64
65
66
67
68
69
70
71
package today_S20;
import java.util.Arrays;
public class make_all_rows_col_as_0s_using_extra_space {
public static void main(String[] args) {
int[][] mat = {{2,3,4,0},{3,5,0,7},{1,2,3,4},{8,2,3,4}};
make_zero(mat);
}
public static void make_zero(int[][] mat) {
if(mat == null || mat.length == 0) {
return;
}
int m = mat.length;
int n = mat[0].length;
boolean row = false, col = false;
for(int i = 0; i < n; i++) {
if(mat[0][i] == 0) {
row = true;
break;
}
}
for(int j = 0;j < m; j++) {
if(mat[j][0] == 0) {
col = true;
break;
}
}
for (int i = 1; i < m; i++)
{
for (int j = 1; j < n; j++)
{
if (mat[i][j] == 0) {
mat[0][j] = mat[i][0] = 0;
}
}
}
for (int i = 1; i < m; i++)
{
for (int j = 1; j < n; j++)
{
if (mat[0][j] == 0 || mat[i][0] == 0) {
mat[i][j] = 0;
}
}
}
for (int i = 0; row && i < n; i++) {
mat[0][i] = 0;
}
// if `colFlag` is true, then assign 0 to all cells of the first column
for (int i = 0; col && i < m; i++) {
mat[i][0] = 0;
}
for (int i = 0; i < m; i++)
{
for (int j= 0; j < n; j++)
{
System.out.print(mat[i][j]+" ");
}
System.out.println();
}
}
}