498. Diagonal Traverse
阿新 • • 發佈:2018-11-01
return ++ add () traverse i+1 nbsp .get arr
1 class Solution { 2 public int[] findDiagonalOrder(int[][] matrix) { 3 if(matrix.length == 0) return new int[0]; 4 int row = matrix.length; 5 int col = matrix[0].length; 6 int i = 0, j = 0; 7 List<Integer> list = new ArrayList<>(); 8 intflag = 1; 9 while(list.size() < row*col){ 10 if(i >= 0 && i < row && j >= 0 && j < col){ 11 // System.out.println(matrix[i][j]); 12 list.add(matrix[i][j]); 13 if(flag == 1){ 14 i = i-1;15 j = j+1; 16 }else{ 17 i = i+1; 18 j = j-1; 19 } 20 }else if(flag == 1){ 21 if(j < col){ 22 i = i+1; 23 }else{ 24 i = i+2; 25 j = j-1;26 27 } 28 flag = - flag; 29 }else if(flag == -1){ 30 if(i >= row){ 31 j = j+2; 32 i = i-1; 33 }else{ 34 j = j+1; 35 } 36 flag = -flag; 37 } 38 } 39 int[] res = new int[row*col]; 40 for(int k = 0; k < res.length; k++){ 41 res[k] = list.get(k); 42 } 43 return res; 44 } 45 }
498. Diagonal Traverse