← 返回 cisco 的题目列表Matrix Zig-Zag Traversal
类型:qbank
Given an `N x M` matrix, print all elements in the zig-zag diagonal order shown by the statement.
Requirements
Input line 1 contains matrix_row and matrix_col.
The next N lines contain M space-separated integers for the matrix.
Print N * M space-separated integers representing the matrix in zig-zag order.
The example traversal starts at the top-left, walks along anti-diagonals, and alternates direction by diagonal.
Examples
Input:
3 3
1 2 3
4 5 6
7 8 9
Output:
1 2 4 7 5 3 6 8 9
Notes
The source names this as a LeetCode 53-style variant, but the prompt's shown order is anti-diagonal zig-zag rather than spiral order. Follow the screenshot's example order.