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.
Examples
01 · 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].
02 · 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⁵
More Meta problems
- Merge Three Sorted ArraysPHONE SCREEN · Seen May 2026
- Find Peak ElementPHONE SCREEN · Seen Mar 2025
- Find Pair Closest to K (for E5 :)PHONE SCREEN · Seen Feb 2025
- Get Minimum Round Trip Cost (: for E4 && E5 :)PHONE SCREEN · Seen Feb 2025
- Max Consecutive Ones III (for E5 :)PHONE SCREEN · Seen Feb 2025
- Nested List Weight Sum (for E4)PHONE SCREEN · Seen Feb 2025
- Sliding Window Average (Meta Canada, E4/E5 :)PHONE SCREEN · Seen Dec 2024
- Build Blocks and Obstacles on a Number LineSeen Oct 2024
public int[] findDiagonalOrder(int[][] mat) {
// write your code here
}
mat[[1,2,3],[4,5,6],[7,8,9]]
expected[1,2,4,7,3,5,8,6,9]
sign in to submit