Who can tell me why it not passed? I tested different kind of matrix


  • 0
    T
    	 public static List<Integer> spiralOrder1(int[][] matrix) {
            int row=matrix.length;
            int col=matrix[0].length;
            List<Integer> res=new ArrayList<Integer>();
            if(matrix==null||row==0||col==0)
            return res;
            int layer=0;
            if(row==1){
                for(int k=0;k<col;k++){
                 res.add(matrix[0][k]);
                }
                 return res;
            }
             if(col==1){
                for(int k=0;k<row;k++){
                 res.add(matrix[k][0]);
                }
                 return res;
            }
            while(layer<(Math.min(row,col)/2)){
                int i=layer;
                int j=layer;
                for(;j<col-1-layer;j++){
                    res.add(matrix[i][j]);
                }
                for(;i<row-1-layer;i++){
                    res.add(matrix[i][j]);
                }
                for(;j>layer;j--){
                    res.add(matrix[i][j]);
                }
                for(;i>layer;i--){
                    res.add(matrix[i][j]);
                }
              layer++; 
            }
            if(res.size()<(row*col))
            res.add(matrix[layer][layer]);
            return res;
        }

Log in to reply
 

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