Google Interview - Print Matrix Diagonally

Given a 2D matrix, print all elements of the given matrix in diagonal order. For example, consider the following 5 X 4 input matrix.

    1     2     3     4
    5     6     7     8
    9    10    11    12
   13    14    15    16
   17    18    19    20

Diagonal printing of the above matrix is

    1
    5     2
    9     6     3
   13    10     7     4
   17    14    11     8
   18    15    12
   19    16
   20
public void printDiagonal(int[][] A) {
    int m = A.length, n = A[0].length;
    for (int i = 0; i < m+n-1; i++) {
        int row = Math.min(i, m-1);
        int col = Math.max(0, i-m+1);
        while(row>=0 && col<n) {
            System.out.print(A[row--][col++] + " ");
        }
        System.out.println();
    }
}

  

猜你喜欢

转载自yuanhsh.iteye.com/blog/2217185