-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetMatrixZeroes73.cs
More file actions
172 lines (152 loc) · 4.23 KB
/
SetMatrixZeroes73.cs
File metadata and controls
172 lines (152 loc) · 4.23 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
namespace LeetCode;
/// <summary>
/// Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0's.
//
// You must do it in place.
//
//
//
// Example 1:
//
//
// Input: matrix = [[1,1,1],[1,0,1],[1,1,1]]
// Output: [[1,0,1],[0,0,0],[1,0,1]]
// Example 2:
//
//
// Input: matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]
// Output: [[0,0,0,0],[0,4,5,0],[0,3,1,0]]
//
//
// Constraints:
//
// m == matrix.length
// n == matrix[0].length
// 1 <= m, n <= 200
// -231 <= matrix[i][j] <= 231 - 1
//
//
// Follow up:
//
// A straightforward solution using O(mn) space is probably a bad idea.
// A simple improvement uses O(m + n) space, but still not the best solution.
// Could you devise a constant space solution?
/// </summary>
public class SetMatrixZeroes73
{
public void SetZeroesNM(int[][] matrix)
{
bool [][] zero = new bool[matrix.Length][];
for(int i=0; i < matrix.Length;i++)
{
zero[i] = new bool[matrix[i].Length];
for (int j = 0; j < matrix[i].Length; j++)
{
zero[i][j] = matrix[i][j] == 0;
}
}
for (int i = 0; i < zero.Length; i++)
{
for (int j = 0; j < zero[i].Length; j++)
{
if(zero[i][j]) ZeroRowColumn(i,j, matrix);
}
}
}
public void SetZeroesRecursive(int[][] matrix)
{
//rows
CheckAllCells(0,0, matrix);
Console.WriteLine("Hello");
}
void CheckAllCells(int row, int column, int[][]matrix)
{
bool zeroed = RowIsZero(row, matrix) || ColumnIsZero(column, matrix);
int r=row;
int c=column;
if (column < matrix[row].Length-1) c ++;
else if (row < matrix.Length - 1)
{
r++;
c = 0;
}
if(row != r || column != c) CheckAllCells(r, c, matrix);
matrix[row][column] = zeroed ? 0 : matrix[row][column];
}
// bool CheckRowCell( bool rowIsZero, bool columnIsZero, int row, int column, int[][] matrix)
// {
// if (column < matrix[row].Length)
// rowIsZero = CheckRowCell(rowIsZero || matrix[row][column] == 0, row, column + 1, matrix);
// if (row < matrix.Length)
//
// }
public void SetZeroesOOne(int[][] matrix)
{
bool firstColIsZero = false;
bool firstRowIsZero = false;
for (int row = 0; row < matrix.Length; row++)
{
for (int col = 0; col < matrix[row].Length; col++)
{
if (matrix[row][col] == 0)
{
if (col == 0) firstColIsZero = true;
if (row == 0) firstRowIsZero = true;
matrix[row][0] = 0;
matrix[0][col] = 0;
}
}
}
for (int row = 1; row < matrix.Length; row++)
{
for (int col = 1; col < matrix[row].Length; col++)
{
if (matrix[row][0] == 0 || matrix[0][col] == 0)
{
matrix[row][col] = 0;
}
}
}
if (firstRowIsZero)
{
for (int col = 0; col < matrix[0].Length; col++)
{
matrix[0][col] = 0;
}
}
if (firstColIsZero)
{
for (int row = 0; row < matrix.Length; row++)
{
matrix[row][0] = 0;
}
}
}
bool RowIsZero(int row, int[][] matrix)
{
for (int i = 0; i < matrix[row].Length; i++)
{
if (matrix[row][i] == 0) return true;
}
return false;
}
bool ColumnIsZero(int column, int[][] matrix)
{
for (int i = 0; i < matrix.Length; i++)
{
if (matrix[i][column] == 0) return true;
}
return false;
}
public void ZeroRowColumn(int row, int column, int[][] matrix)
{
for (int i = 0; i < matrix[0].Length; i++)
{
matrix[row][i] = 0;
}
for (int i = 0; i < matrix.Length; i++)
{
matrix[i][column] = 0;
}
}
}