Without using magic number

public int[] findDiagonalOrder(int[][] matrix) { int m = matrix.length; if(m == 0) return new int[0]; int n = matrix[0].length; int[] res = new int[m * n]; String dir = "RU"; int r = 0, c = 0; for(int i = 0; i < m * n; i++){ res[i] = matrix[r][c]; if(dir.equals("RU")){ r--; c++; } else { r++; c--; } if(r >= m) { r = m - 1; c += 2; dir = "RU"; } if(c >= n) { c = n - 1; r += 2; dir = "LD"; } if(r < 0){ r = 0; dir = "LD"; } if(c < 0){ c = 0; dir = "RU"; } } return res; }Diagonal traverse