Straightforward java solution


  • 6
    L
    public class Solution {
    public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> list = new ArrayList<Integer>();
        if(matrix == null || matrix.length == 0 || matrix[0].length == 0) return list;
        int row = matrix.length;
        int col = matrix[0].length;
        int left = 0, right = col-1;
        int top = 0, bottom = row-1;
        while(true){
            for(int i = left; i<=right; i++){
                list.add(matrix[top][i]);
            }
            top++;
            if(top > bottom) break;
            for(int i = top; i<=bottom; i++){
                list.add(matrix[i][right]);
            }
            right--;
            if(right < left) break;
            for(int i = right; i>=left; i--){
                list.add(matrix[bottom][i]);
            }
            bottom--;
            if(bottom < top) break;
            for(int i = bottom; i>=top; i--){
                list.add(matrix[i][left]);
            }
            left++;
            if(left > right) break;
        }
        return list;
    }
    

    }


  • 0
    A

    @linwei2 What is the running time for this algorithm?


  • 0
    S

    @acheiver O(n*m) where n and m are number of rows and columns respectively


  • 0
    A

    @sameer13 basically we end up visiting all nodes in the matrix, so it is n*m?


  • 1
    S

    @acheiver Yes. You visit each element once.


Log in to reply
 

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