#1424

Diagonal Traverse II

specialist · 740 · lc medium +31 · verified · 58.3% accepted · 2,298 likes · top 54%

play →

Description

Given a 2D integer array nums (rows may have different lengths), return all elements traversed in diagonal order: group elements by their i + j sum (same diagonal), and within each diagonal collect elements in order of increasing column index j.

Example 1:

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

Example 2:

Input: nums = [[1,2,3,4,5],[6,7],[8],[9,10,11],[12,13,14,15,16]]
Output: [1,6,2,8,7,3,9,4,12,10,5,13,11,14,15,16]

Code

1
2
3