Matrix Diagonal Traverse
Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal order as shown in the below image.
Example:
Input:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
Output: [1,2,4,7,5,3,6,8,9]
Note
int[][] dirs = {{-1, 1}, {1, -1}}表示右上和左下
在出界的时候进行判断:
如果上边出界:行数置为0,反向
如果左边出界:列数置为0,反向
如果下边出界:列数减2,反向
如果右边出界:行数减2,反向
Code
Last updated