forked from anishLearnsToCode/leetcode-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SetMatrixZeroes.java
46 lines (39 loc) · 1.33 KB
/
SetMatrixZeroes.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
// https://leetcode.com/problems/set-matrix-zeroes/
// T: O(m * n)
// S: O(1)
public class SetMatrixZeroes {
public void setZeroes(int[][] matrix) {
final int rows = matrix.length, columns = matrix[0].length;
boolean firstColumnIsZero = false;
for (int row = 0 ; row < rows ; row++) {
if (matrix[row][0] == 0) firstColumnIsZero = true;
for (int column = 1 ; column < columns ; column++) {
if (matrix[row][column] == 0) {
matrix[row][0] = 0;
matrix[0][column] = 0;
}
}
}
for (int column = columns - 1 ; column >= 1 ; column--) {
if (matrix[0][column] == 0) {
markColumn0(matrix, column);
}
}
for (int row = rows - 1 ; row >= 0 ; row--) {
if (matrix[row][0] == 0) {
markRow0(matrix, row);
}
}
if (firstColumnIsZero) markColumn0(matrix, 0);
}
private void markRow0(int[][] matrix, int row) {
for (int column = 0 ; column < matrix[0].length ; column++) {
matrix[row][column] = 0;
}
}
private void markColumn0(int[][] matrix, int column) {
for (int row = 0 ; row < matrix.length ; row++) {
matrix[row][column] = 0;
}
}
}