Google | Phone Screen | Print Matrix Diagonally
6678

Given a matrix, return all elements of the matrix in diagonal order.

Example 1:

Input: [[ 1,  2,  3,  4],
        [ 6,  7,  8,  9],
        [11, 12, 13, 14],
        [16, 17, 18, 19]]

Output: [1, 6, 2, 11, 7, 3, 16, 12, 8, 4, 17, 13, 9, 18, 14, 19]
Explanation:

Example 2:

Input: [[1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]]

Output: [1, 4, 2, 7, 5, 3, 8, 6, 9]

Follow-up:
What if the input is a jugged array (rows have different column sizes)?

Example:

Input: [[1, 2, 3, 4],
        [5         ],
        [6, 7      ],
        [8, 9, 0   ]]

Output: [1, 5, 2, 6, 3, 8, 7, 4, 9, 0]

Related problems:

Comments (19)