forked from hongtaocai/code_interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetmatrixzeros.java
More file actions
executable file
·51 lines (51 loc) · 1.45 KB
/
setmatrixzeros.java
File metadata and controls
executable file
·51 lines (51 loc) · 1.45 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
public class Solution {
public void setZeroes(int[][] matrix) {
// Start typing your Java solution below
// DO NOT write main() function
if(matrix.length==0){
return;
}
int zerorow = 0;
loop1: for(zerorow=0;zerorow<matrix.length;zerorow++){
for(int j=0;j<matrix[zerorow].length;j++){
if(matrix[zerorow][j]==0) break loop1;
}
}
if(zerorow==matrix.length){
return;
}
for(int i=0;i<matrix[0].length;i++){
for(int j=zerorow;j<matrix.length;j++){
if(matrix[j][i]==0){
matrix[zerorow][i]=0;
break;
}
}
}
int flag = 0;
for(int i=zerorow+1;i<matrix.length;i++){
flag = 0;
for(int j=0;j<matrix[0].length;j++){
if(matrix[i][j]==0){
flag = 1;
break;
}
}
if(flag==1){
for(int j=0;j<matrix[0].length;j++){
matrix[i][j]=0;
}
}
}
for(int i=0;i<matrix[0].length;i++){
if(matrix[zerorow][i]==0){
for(int j=0;j<matrix.length;j++){
matrix[j][i]=0;
}
}
}
for(int i=0;i<matrix[0].length;i++){
matrix[zerorow][i]=0;
}
}
}