54. Spiral Matrix

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.

For example, Given the following matrix:

[
 [ 1, 2, 3 ],
 [ 4, 5, 6 ],
 [ 7, 8, 9 ]
]

You should return [1,2,3,6,9,8,7,4,5].

Related issue Spiral Matrix II

You surely can used 'peel-onion' to do this, first your will know the peeling stop at n/2, m/2, then another round when there is a row /col left. a better solution is to set stop coordinates.

public class Solution {
    public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> res = new ArrayList<>();
        if(matrix == null || matrix.length == 0 ||matrix[0].length == 0) return res;

        int m = matrix.length;
        int n = matrix[0].length;
        int p1 = 0, q1 = 0;
        int p2 = m-1, q2 = n-1;

        while(true){
            for(int k= q1; k<=q2; k++){
                res.add(matrix[p1][k]);
            }
            if(++p1 > p2) break;

            for(int k=p1; k <=p2;k++){
                res.add(matrix[k][q2]);
            }
            if(--q2 < q1) break;

            for(int k=q2; k>=q1;k--){
                res.add(matrix[p2][k]);
            }
            if(--p2 < p1) break;

            for(int k=p2; k>=p1; k--){
                res.add(matrix[k][q1]);
            }
            if(++q1 > q2) break;
        }
        return res;
    }
}

results matching ""

    No results matching ""