-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetZeroesMatrix.java
More file actions
40 lines (32 loc) · 805 Bytes
/
SetZeroesMatrix.java
File metadata and controls
40 lines (32 loc) · 805 Bytes
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
package leetcode.practice;
public class SetZeroesMatrix {
public static void main(String[] args) {
int mat[][] = {{1,1,1}, {1,0,1}, {1,1,1}};
MatrixUtils.printMatrix(mat);
MatrixUtils.printMatrix(setZeroes(mat));
}
private static int[][] setZeroes(int[][] mat) {
int rows = mat.length; int cols = mat[0].length;
int[][] newMat = mat;
for(int r=0; r<rows; r++) {
for(int c=0; c<cols; c++) {
if(mat[r][c] == 0) {
updateMatrix(newMat,r,c);
}
}
}
return newMat;
}
private static void updateMatrix(int[][] newMat, int r, int c) {
int i = r; int j=c;
int k = r; int l=c;
while(i < newMat.length && j < newMat[0].length){
newMat[i++][c] = 0;
newMat[r][j++] = 0;
}
while(k > 0 && l > 0){
newMat[--k][c] = 0;
newMat[r][--l] = 0;
}
}
}