Clean Java Code


  • 0
    4
        public List<Integer> spiralOrder(int[][] matrix) {
            List<Integer> list = new ArrayList<>();
            if(matrix == null || matrix.length == 0)    return list;
            int m0 = 0, n0 = 0, m1 = matrix.length - 1, n1 = matrix[0].length - 1;
            int i = 0, j = 0;
            list.add(matrix[i][j]);
            while(m0 <= m1 && n0 <= n1) {
                while(j < n1)  list.add(matrix[i][++j]);
                m0++;
                if(m0 > m1) break;
                while(i < m1)  list.add(matrix[++i][j]);
                n1--;
                if(n0 > n1) break;
                while(j > n0)  list.add(matrix[i][--j]);
                m1--;
                while(i > m0)  list.add(matrix[--i][j]);
                n0++;
            }
            return list;
        }
    

Log in to reply
 

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