题解 | #顺时针打印矩阵#
顺时针打印矩阵
http://www.nowcoder.com/practice/9b4c81a02cd34f76be2659fa0d54342a
//就是每次将上行,右列,下行,左列 都加进去 让list去重 ,然后top++ ; right -- ,bottom-- ,left++, 直到top > bottom 或者 left > right 时退出循环就ok了(简单易懂~~~) ArrayList<Integer> list = new ArrayList<>(); int top = 0 ; int bottom = matrix.length-1; int left = 0; int right = matrix[0].length -1; while(top <= bottom && left <= right){ //上 for(int i = left ; i<=right; i++){ if(!list.contains(matrix[top][i])){ list.add(matrix[top][i]) ; } } //右 for(int i = top ; i<=bottom; i++){ if(!list.contains(matrix[i][right])){ list.add(matrix[i][right]) ; } } //下 for(int i = right ; i>=left; i--){ if(!list.contains(matrix[bottom][i])){ list.add(matrix[bottom][i]) ; } } //左 for(int i = bottom ; i>=top; i--){ if(!list.contains(matrix[i][left])){ list.add(matrix[i][left]) ; } } top++; bottom--; left++; right--; } return list; }