Last executed input: [] ,so what i should return ? I know it's empty,but I really don't kown how to deal
Last executed input: [] ,so what i should return ? I know it's empty,but I really don't kown how to deal

If matrix is empty you cannot get the first row.
To get the first row of the matrix is better to write matrix.front(), is more expressive, for the same reason you call matrix.size() instead of distance(matrix.begin(), matrix.end()) to know the number of rows.
Number of columns: matrix.front().size() ;)

Please check the beginning part of my code. It returns a List<Integer> that does not contain any element.
public List<Integer> spiralOrder(int[][] matrix) { List<Integer> output = new ArrayList<Integer>(); int minY = 1; int maxY = matrix.length  1; if (maxY < 0) return output; int minX = 0; int maxX = matrix[0].length 1; if (maxX < 0) return output; int N = matrix.length * matrix[0].length; int i = 0; int j = 0; int dir = 0; for (int count = 0; count < N; ++count) { output.add(matrix[i][j]); switch(dir%4) { case 0: // right if (j < maxX) { j++; continue; } dir++; maxX; case 1: // down if (i < maxY) { i++; continue; } dir++; maxY; case 2: // left if (j > minX) { j; continue; } dir++; minX++; case 3: // up if (i > minY) { i; continue; } dir++; minY++; j++; } } return output; }