Problem Brief
Diagonal Traverse (for E4 ;)
FULLTIMEPHONE SCREEN
LC 498~ The other question was LC 1539~~
Given an m × n matrix mat, return an array of all the elements of the array in a diagonal order.
1Example 1
Input
mat = [[1,2,3],[4,5,6],[7,8,9]]
Output
[1,2,4,7,3,5,8,6,9]
Explanation
Traverse the matrix diagonally as follows:
- Start from element
1 - Move up-right to reach
2, then down-left to4 - Move up-right to reach
7, then down-right to3 - Continue the pattern to traverse all elements diagonally
[1,2,4,7,3,5,8,6,9].
2Example 2
Input
mat = [[1,2],[3,4]]
Output
[1,2,3,4]
Explanation
Traverse the matrix diagonally as follows:
- Start from element
1 - Move up-right to reach
2, then down-left to3 - Finally, move up-right to reach
4
[1,2,3,4].
Constraints
Limits and guarantees your solution can rely on.
m == mat.lengthn == mat[i].length1 <= m, n <= 10⁴1 <= m * n <= 10⁴-10⁵ <= mat[i][j] <= 10⁵