Matrix Zigzag Traversal
Source
Given a matrix of m x n elements (m rows, n columns), return all elements
of the matrix in ZigZag-order.
Given a matrix:
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9,10, 11, 12]
]
return [1, 2, 5, 9, 6, 3, 4, 7, 10, 11, 8, 12]
java
public class Solution {
public int[] printZMatrix(int[][] matrix) {
if(matrix == null || matrix.length == 0 || matrix[0].length == 0) return null;
int count = matrix.length * matrix[0].length;
int[] array = new int[count];
int r = 0, c = 0;
array[0] = matrix[0][0];
for (int i = 1; i < count; ) {
while(i < count && r - 1 >= 0 && c + 1 < matrix[0].length) {
array[i++] = matrix[--r][++c];
}
if (i < count && c + 1 < matrix[0].length) {
array[i++] = matrix[r][++c];
} else if (i < count && r + 1 < matrix.length) {
array[i++] = matrix[++r][c];
}
while(i < count && r + 1 < matrix.length && c - 1 >= 0) {
array[i++] = matrix[++r][--c];
}
if (i < count && r + 1 < matrix.length) {
array[i++] = matrix[++r][c];
} else if (i < count && c + 1 < matrix[0].length) {
array[i++] = matrix[r][++c];
}
}
return array;
}
}