-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiagonalTraversalOfMatrix.java
More file actions
62 lines (52 loc) · 1.55 KB
/
DiagonalTraversalOfMatrix.java
File metadata and controls
62 lines (52 loc) · 1.55 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
package Leetcode;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class DiagonalTraversalOfMatrix
{
public static List<List<Integer>> diagonalTraversal( int[][] A )
{
List<List<Integer>> result = new ArrayList<>();
int N = A.length;
int M = A[0].length;
int count = 0;
for( int i = 0; i < M; i++ )
{
count++;
int row = 0;
int col = i;
List<Integer> currentResult = new ArrayList<>();
while( row < N && col >= 0 )
{
currentResult.add( A[row][col] );
row++;
col--;
}
if( count % 2 != 0 )
Collections.reverse( currentResult );
result.add( currentResult );
}
for(int i=1;i<M;i++)
{
count++;
int row=i;
int col = N-1;
List<Integer> currentResult = new ArrayList<>();
while(row<M && col>0)
{
currentResult.add( A[row][col] );
row++;
col--;
}
if(count%2!=0)
Collections.reverse( currentResult );
result.add( currentResult );
}
return result;
}
public static void main( String[] args )
{
int[][] A = new int[][]{ { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 }, { 13, 14, 15, 16 } };
System.out.println( diagonalTraversal( A ) );
}
}