Talentd
FleetCode

498. Diagonal Traverse

Medium67.1% acceptance12 companies

Given an m x n matrix mat, return an array of all the elements of the array in a diagonal order.

Example 1:

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

Example 2:

Input: mat = [[1,2],[3,4]]
Output: [1,2,3,4]

Constraints:

  • m == mat.length
  • n == mat[i].length
  • 1 <= m, n <= 104
  • 1 <= m * n <= 104
  • -105 <= mat[i][j] <= 105
6:56
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]:
3
Starter codePython 3Spaces: 4
Examples
Input
[[1,2,3],[4,5,6],[7,8,9]]
Input
[[1,2],[3,4]]
Your code is saved on this device.Solution