-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSet Matrix Zeroes.java
121 lines (104 loc) · 2.98 KB
/
Set Matrix Zeroes.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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// https://leetcode.com/problems/set-matrix-zeroes/
// Attached solutions of three different approaches of different complexities
// Time - O(MN) Space - O(M+N)
class Solution {
public void setZeroes(int[][] matrix) {
int row[] = new int [matrix[0].length];
int col[] = new int [matrix.length];
for(int i = 0; i < matrix.length; i++)
{
for(int j = 0; j < matrix[0].length; j++)
{
if(matrix[i][j] == 0)
{
row[j] = 1;
col[i] = 1;
}
}
}
for(int i = 0; i < matrix.length; i++)
{
for(int j = 0; j < matrix[0].length; j++)
{
if(col[i] == 1 || row[j] == 1)
matrix[i][j] = 0;
}
}
}
}
// Time - O(MN) Space - O(MN)
class Solution {
class Pair{
int x, y;
Pair(int x, int y)
{
this.x = x;
this.y = y;
}
}
public void setZeroes(int[][] matrix) {
List<Pair> map = new ArrayList<>();
for(int i = 0; i < matrix.length; i++)
{
for(int j = 0; j < matrix[0].length; j++)
{
if(matrix[i][j] == 0)
map.add(new Pair(i,j));
}
}
for(Pair pair: map)
setzero(matrix, pair.x, pair.y);
}
void setzero(int [][]a, int m, int n)
{
for(int i = 0; i < a.length; i++)
a[i][n] = 0;
for(int i = 0; i < a[0].length; i++)
a[m][i] = 0;
}
}
// Time - O(MN) Space - O(1)
class Solution {
public void setZeroes(int[][] matrix) {
int row = 0, col = 0;
for(int i = 0; i < matrix.length; i++)
{
if(matrix[i][0] == 0) { col = 1; break; }
}
for(int i = 0; i < matrix[0].length; i++)
{
if(matrix[0][i] == 0) { row = 1; break; }
}
for(int i = 1; i < matrix.length; i++)
{
for(int j = 1; j < matrix[0].length; j++)
{
if(matrix[i][j] == 0)
matrix[0][j] = matrix[i][0] = 0;
}
}
for(int i = 0; i < matrix.length; i++)
{
for(int j = 0; j < matrix[0].length; j++)
{
System.out.print(matrix[i][j]+" ");
}
System.out.println();
}
System.out.println();
for(int i = 1; i < matrix.length; i++)
{
for(int j = 1; j < matrix[0].length; j++)
{
if(matrix[0][j] == 0 || matrix[i][0] == 0)
matrix[i][j] = 0;
}
}
if(col == 1)
for(int i = 0; i < matrix.length; i++)
matrix[i][0] = 0;
if(row == 1)
for(int i = 0; i < matrix[0].length; i++)
matrix[0][i] = 0;
}
}