classSolution{ public List<Integer> spiralOrder(int[][] matrix){ //result List<Integer> result = new ArrayList<>(); if (matrix.length == 0) return result; //获取行数---Y行 int lenY =matrix.length; //获取列数---X列 int lenX = matrix[0].length; String direction = "right"; int left=0; int right = lenX-1; int down =0; int up = lenY-1; while(left<=right && down<=up){ if(direction == "right"){ for(int i=left;i<=right;i++){ result.add(matrix[down][i]); } down++; direction = "down"; }elseif(direction=="down"){ for(int i=down;i<=up;i++){ result.add(matrix[i][right]); } right--; direction = "left"; }elseif(direction=="left"){ for(int i=right;i>=left;i--){ result.add(matrix[up][i]); } up--; direction ="up"; }elseif(direction=="up"){ for(int i=up;i>=down;i--){ result.add(matrix[i][left]); } left++; direction = "right"; } } return result; } }