-
Notifications
You must be signed in to change notification settings - Fork 0
/
SetMatrixZeroes.java
49 lines (43 loc) · 1.07 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
47
48
49
package matrix;
/**
* @author Shogo Akiyama
* Solved on 08/13/2019
*
* 73. Set Matrix Zeroes
* https://leetcode.com/problems/set-matrix-zeroes/
* Difficulty: Medium
*
* Approach: Iteration with O(m + n) space
* Runtime: 1 ms, faster than 100.00% of Java online submissions for Set Matrix Zeroes.
* Memory Usage: 41.8 MB, less than 97.14% of Java online submissions for Set Matrix Zeroes.
*
* @see MatrixTest#testSetMatrixZeroes()
*/
public class SetMatrixZeroes {
public void setZeroes(int[][] matrix) {
int[] row = new int[matrix[0].length];
int[] column = 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;
column[i] = 1;
}
}
}
for (int k = 0; k < row.length; k++) {
if (row[k] == 1) {
for (int c = 0; c < matrix.length; c++) {
matrix[c][k] = 0;
}
}
}
for (int l = 0; l < column.length; l++) {
if (column[l] == 1) {
for (int r = 0; r < matrix[0].length; r++) {
matrix[l][r] = 0;
}
}
}
}
}