-
Notifications
You must be signed in to change notification settings - Fork 115
/
073-SetMatrixZeroes.cs
65 lines (56 loc) · 2.14 KB
/
073-SetMatrixZeroes.cs
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
//-----------------------------------------------------------------------------
// Runtime: 184ms
// Memory Usage:
// Link:
//-----------------------------------------------------------------------------
namespace LeetCode
{
public class _073_SetMatrixZeroes
{
public void SetZeroes(int[,] matrix)
{
var rowLength = matrix.GetLength(0);
var columnLenght = matrix.GetLength(1);
var rowZero = new bool[rowLength];
var columnZero = new bool[columnLenght];
int i, j;
for (i = 0; i < rowLength; i++)
for (j = 0; j < columnLenght; j++)
if (matrix[i, j] == 0)
{
rowZero[i] = true;
columnZero[j] = true;
}
for (i = 0; i < rowLength; i++)
for (j = 0; j < columnLenght; j++)
if (rowZero[i] || columnZero[j])
matrix[i, j] = 0;
}
public void SetZeroes_2(int[,] matrix)
{
var rowLength = matrix.GetLength(0);
var columnLenght = matrix.GetLength(1);
var firstRowHasZero = false;
var firstColumnHasZero = false;
int i, j;
for (i = 0; i < rowLength; i++)
if (matrix[i, 0] == 0) { firstColumnHasZero = true; break; }
for (i = 0; i < columnLenght; i++)
if (matrix[0, i] == 0) { firstRowHasZero = true; break; }
for (i = 1; i < rowLength; i++)
for (j = 1; j < columnLenght; j++)
if (matrix[i, j] == 0)
matrix[0, j] = matrix[i, 0] = 0;
for (i = 1; i < rowLength; i++)
for (j = 1; j < columnLenght; j++)
if (matrix[i, 0] == 0 || matrix[0, j] == 0)
matrix[i, j] = 0;
if (firstRowHasZero)
for (i = 0; i < columnLenght; i++)
matrix[0, i] = 0;
if (firstColumnHasZero)
for (i = 0; i < rowLength; i++)
matrix[i, 0] = 0;
}
}
}