-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSetZero.java
More file actions
76 lines (64 loc) · 1.58 KB
/
SetZero.java
File metadata and controls
76 lines (64 loc) · 1.58 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package edu.java.chap1;
import java.util.LinkedList;
/*
* Write an algorithm such that if an element in an M*N matrix is 0, its entire row and column are set to 0.
*
*/
public class SetZero {
public static void matrixPrint(int[][] matrix){
System.out.println();
for(int i =0; i < matrix.length;i++){
for(int j = 0; j <matrix[0].length;j++){
System.out.print(matrix[i][j]+" ");
}
System.out.println();
}
}
public static int[][] set0(int[][] arr){
int rowL = arr[0].length;
int columnL = arr.length;
int[][] result = new int[columnL][rowL];
LinkedList<Integer> list = new LinkedList<Integer>();
outer:
for(int i = 0; i<columnL; i++){
for(int j = 0;j<rowL; j++ ){
//System.out.println(i+" "+j);
if(check(j,list)){
result[i][j] = 0;
}
else if (arr[i][j] == 0){
for(int k = 0; k<j+1;k++){
result[i][k] = 0;
}
for(int k = 0;k<i+1;k++){
result[k][j] = 0;
}
list.add(j);
continue outer;
}
else{
result[i][j] = arr[i][j];
}
}
}
return result;
}
public static boolean check(int i, LinkedList<Integer> list){
for(int j = 0; j < list.size(); j++){
if(i == list.get(j).intValue()){
return true;
}
}
return false;
}
public static int[][] setZero(int[][] arr){
int[][] result = new int[arr.length][arr[0].length];
//use two boolean[] to store the position, use O(N^2)
return result;
}
public static void main(String[] args) {
int[][] arr = {{1,2,3,0},{2,3,0,1},{1,2,3,4},{2,3,4,5},{0,1,2,3}};
matrixPrint(arr);
matrixPrint(set0(arr));
}
}