> For the complete documentation index, see [llms.txt](https://emmaguo100.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://emmaguo100.gitbook.io/leetcode/02-07-2022-54.md).

# 02/07/2022 54

This problem can be solved with s similar algorithm to the 59th problem. But the tricky part is when you traverse from right to left, from bottom to up, you have to check if (left > right || top > bottom) to avoid duplicate elements.

Time O(n)

Space O(1)

```
    class Solution { 
    public List spiralOrder(int[][] matrix) { 
    if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { 
    return new ArrayList<>(); } 
    List<Integer> result = new ArrayList<>(); 
    int rowSize = matrix.length; 
    int columnSize = matrix[0].length; 
    int left = 0, right = columnSize - 1; 
    int top = 0, bottom = rowSize - 1;
    
    
    while(left <= right && top <= bottom){// We need not check whether it exceeded 
    the bounds because after one iteration while loop condition is checked again. 
    So, this line does the job while traversing from left to right and from top 
    to down.
        //traverse from left to right
        for(int j = left; j <= right; j++){
            result.add(matrix[top][j]);
        }
        top++;
        //traver from top to bottom
        for(int i = top; i <= bottom; i++){
            result.add(matrix[i][right]);
        }
        right--;
        
        if(top > bottom || left > right) break;
        //traverse from right to left;
        for(int j = right; j >= left; j--){
            if(top <= bottom){
                result.add(matrix[bottom][j]);
            }       
        }
        bottom--;
        
        for(int i = bottom; i >= top; i--){
            if(left <= right){
               result.add(matrix[i][left]); 
            }
            
        }
        left++;
    }
    return result;
}
```

}

}

```
    class Solution {
    public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> result = new ArrayList<>();
        int rowSize = matrix.length;
        int columnSize = matrix[0].length;
        int left = 0, right = columnSize - 1;
        int top = 0, bottom = rowSize - 1;
        
        while(left <= right && top <= bottom){
            //traverse from left to right
            for(int j = left; j <= right; j++){
                result.add(matrix[top][j]);
            }
            top++;
            //traver from top to bottom
            for(int i = top; i <= bottom; i++){
                result.add(matrix[i][right]);
            }
            right--;
            
            //traverse from right to left;
            for(int j = right; j >= left; j--){
                if(top <= bottom){
                    result.add(matrix[bottom][j]);
                }       
            }
            bottom--;
            
            for(int i = bottom; i >= top; i--){
                if(left <= right){
                   result.add(matrix[i][left]); 
                }
                
            }
            left++;
        }
        return result;
    }
}
    while(left <= right && top <= bottom){
        //traverse from left to right
        for(int j = left; j <= right; j++){
            result.add(matrix[top][j]);
        }
        top++;
        //traver from top to bottom
        for(int i = top; i <= bottom; i++){
            result.add(matrix[i][right]);
        }
        right--;
        
        //traverse from right to left;
        for(int j = right; j >= left; j--){
            if(top <= bottom){
                result.add(matrix[bottom][j]);
            }       
        }
        bottom--;
        
        for(int i = bottom; i >= top; i--){
            if(left <= right){
               result.add(matrix[i][left]); 
            }
            
        }
        left++;
    }
    return result;
}
```

}

```
class Solution {
    public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> res = new LinkedList<>(); 
        if (matrix == null || matrix.length == 0) return res;
        int n = matrix.length, m = matrix[0].length;
        int up = 0,  down = n - 1;
        int left = 0, right = m - 1;
        while (res.size() < n * m) {
            for (int j = left; j <= right && res.size() < n * m; j++)
                res.add(matrix[up][j]);
            
            for (int i = up + 1; i <= down - 1 && res.size() < n * m; i++)
                res.add(matrix[i][right]);
                     
            for (int j = right; j >= left && res.size() < n * m; j--)
                res.add(matrix[down][j]);
                        
            for (int i = down - 1; i >= up + 1 && res.size() < n * m; i--) 
                res.add(matrix[i][left]);
                
            left++; right--; up++; down--; 
        }
        return res;
    }
}
```
