-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiagonal.java
More file actions
68 lines (50 loc) · 1010 Bytes
/
Copy pathDiagonal.java
File metadata and controls
68 lines (50 loc) · 1010 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
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
/**
Give a N*N square matrix, return an array of its anti-diagonals. Look at the example for more details.
Example:
Input:
1 2 3
4 5 6
7 8 9
Return the following :
[
[1],
[2, 4],
[3, 5, 7],
[6, 8],
[9]
]
Input :
1 2
3 4
Return the following :
[
[1],
[2, 3],
[4]
]
*/
public class Solution {
public ArrayList<ArrayList<Integer>> diagonal(ArrayList<ArrayList<Integer>> a) {
int len = a.get(0).size();
int i = 0;
int j = 0;
int l = 0;
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
for(l=0; l < len; l++){
ArrayList<Integer> iR = new ArrayList<Integer>();
for(i=l;i>=0;i--){
iR.add(a.get(l-i).get(i));
}
res.add(iR);
}
l = len - 1 ;
for(i=0;i<l;i++){
ArrayList<Integer> iR = new ArrayList<Integer>();
for(j=0;j+i<l;j++){
iR.add(a.get(i+j+1).get(l-j));
}
res.add(iR);
}
return res;
}
}