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


  • 0
    C

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


  • 2
    S

    It should return an empty array, which means length of the array is none, but the array should be created.


  • 0
    C

    Thank you very much, I just return *Matrix.begin(), I think if it's empty,it's nice to return *Matrix.begin(). now I know I'm wrong, But why?


  • 0
    S

    Matrix is empty, so there is no element in it. And begin() is for getting the first element. I am not sure which language you are using, I think this call will bring runtime error.


  • 0
    R

    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() ;-)


  • 1
    S

    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;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.