Problem · Matrix
Diagonal Traverse (for E4 ;)
Learn this problemProblem statement
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.
Function
findDiagonalOrder(mat: int[][]) → int[]Examples
Example 1
mat = [[1,2,3],[4,5,6],[7,8,9]]return = [1,2,4,7,3,5,8,6,9]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].
Example 2
mat = [[1,2],[3,4]]return = [1,2,3,4]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
m == mat.lengthn == mat[i].length1 <= m, n <= 10⁴1 <= m * n <= 10⁴-10⁵ <= mat[i][j] <= 10⁵