# clean and fast from-start-to-end traversal java solution

• and i added some debug info with system.out:

``````import java.util.Arrays;

public class Solution {
public int[] findDiagonalOrder(int[][] matrix) {
int m = matrix.length, n = m == 0 ? 0 : matrix[0].length, result[] = new int[m * n], index = 0;

for (int i = 0; i < m + n - 1; i++) {
int startX = (i & 1) == 0 ? Math.min(i, m - 1) : Math.max(0, i - n + 1);
int endX = (i & 1) == 0 ? Math.max(0, i - n + 1) : Math.min(i, m - 1);
int delta = (i & 1) == 0 ? -1 : 1;

System.out.print(Arrays.toString(new int[]{startX, i - startX}));
if (delta == -1) {
System.out.print(" ↗ ");
} else {
System.out.print(" ↙ ");
}
System.out.println(Arrays.toString(new int[]{endX, i - endX}));

while (startX != endX) {
result[index++] = matrix[startX][i - startX];
startX += delta;
}
result[index++] = matrix[endX][i - endX];
}

return result;
}

public static void main(String[] args) {
Solution solution = new Solution();
System.out.println(Arrays.toString(solution.findDiagonalOrder(new int[][]{
new int[]{1, 2, 3},
new int[]{4, 5, 6},
new int[]{7, 8, 9}
})));
}
}
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.