-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathantiDiagnols.java
24 lines (22 loc) · 935 Bytes
/
antiDiagnols.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
/* Give a N*N square matrix, return an array of its anti-diagonals. Look at the example for more details.
*/
public ArrayList<ArrayList<Integer>> diagonal(ArrayList<ArrayList<Integer>> a) {
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
if(a.isEmpty())
return result;
for(int i =0; i<a.size();i ++) {
for (int j = 0; j < a.get(i).size(); j++) {
if(i+j>result.size()-1)
{
ArrayList<Integer> arrayList = new ArrayList<Integer>();
arrayList.add(a.get(i).get(j));
result.add(i + j, arrayList);
}
else
{ ArrayList<Integer> list =result.get(i+j);
list.add(a.get(i).get(j));
}
}
}
return result;
}