Given an m x n matrix, return all elements of the matrix in spiral order.

&nbsp;
Example 1:

Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output: [1,2,3,6,9,8,7,4,5]


Example 2:

Input: matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
Output: [1,2,3,4,8,12,11,10,9,5,6,7]


&nbsp;
Constraints:


	m == matrix.length
	n == matrix[i].length
	1 &lt;= m, n &lt;= 10
	-100 &lt;= matrix[i][j] &lt;= 100


class Solution:
    def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
        col, row = len(matrix[0]), len(matrix)
        l, t, r, b = 0, 0, col - 1, row - 1
        res = []
        while l <= r and t <= b:
            for i in range(l, r):
                res.append(matrix[t][i])
            for i in range(t, b):
                res.append(matrix[i][r])
            
			# Append the orphan left by the open interval
            if t == b:
                res.append(matrix[t][r])
            else:
                # From right to left at the bottom
                for i in range(r, l, -1):
                    res.append(matrix[b][i])
            
			# Avoid duplicated appending if it is a square
            if l == r and t != b:
                res.append(matrix[b][r])
            else:
                # From bottom to top at the left
                for i in range(b, t, -1):
                    res.append(matrix[i][l])
            l += 1
            t += 1
            r -= 1
            b -= 1

        return res
class Solution {
    public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> ans = new ArrayList<>();
        int top = 0, left = 0, bottom = matrix.length - 1, right = matrix[0].length - 1;

        while (top <= bottom && left <= right) 
        {
            for (int i = left; i <= right; i++)
                ans.add(matrix[top][i]);
            top++;

            for (int i = top; i <= bottom; i++)
                ans.add(matrix[i][right]);
            right--;

            if (top <= bottom) {
                for (int i = right; i >= left; i--)
                    ans.add(matrix[bottom][i]);
                bottom--;
            }

            if (left <= right) {
                for (int i = bottom; i >= top; i--)
                    ans.add(matrix[i][left]);
                left++;
            }
        }
        return ans;
    }
}
class Solution {
public:

    vector<int> _res;
    vector<vector<bool>> _visited;

    void spin(vector<vector<int>>& matrix, int direction, int i, int j) {

        _visited[i][j] = true;
        _res.push_back(matrix[i][j]);

        switch (direction){
            // left to right
            case 0:
                if ( j+1 >= matrix[0].size() || _visited[i][j+1]) {
                    direction = 1;
                    i++;
                } else {
                    j++;
                }
                break;
            // up to bottom
            case 1:
                if ( i+1 >= matrix.size() || _visited[i+1][j]) {
                    direction = 2;
                    j--;
                } else {
                    i++;
                }
                break;
            // right to left
            case 2:
                if ( j == 0 || _visited[i][j-1]) {
                    direction = 3;
                    i--;
                } else {
                    j--;
                }
                break;
            // bottom to up
            case 3:
                if ( i == 0 || _visited[i-1][j]) {
                    direction = 0;
                    j++;
                } else {
                    i--;
                }
                break;
        }
        if ( i < 0 || i >= matrix.size() || j < 0 || j >= matrix[0].size() ) {
            return;
        }
        if ( _visited[i][j] ) {
            return;
        }
        spin(matrix, direction, i, j);
    }
    
    vector<int> spiralOrder(vector<vector<int>>& matrix) {
        _res.clear();
        _visited = vector<vector<bool>>(matrix.size(), std::vector<bool>(matrix[0].size(), false));
        spin(matrix, 0, 0, 0);
        return _res;
    }
};
/**
 * @param {number[][]} matrix
 * @return {number[]}
 */
var spiralOrder = function(matrix) {
    let [top, bot, left, right, ans] = [0, matrix.length - 1, 0, matrix[0].length - 1,[]]

    while ((top <= bot) && (left <= right)) {
        for (let j = left; j <= right; j++) {
            ans.push(matrix[top][j])
        }
        top ++;
        for (let i = top; i <= bot; i++) {
            ans.push(matrix[i][right])
        }
        right --;
        if ((bot < top) || (right < left)) {
            break
        }
        for (let j = right; left <= j; j--){
            ans.push(matrix[bot][j])
        }
        bot --;
        for (let i = bot; top <= i; i--){
            ans.push(matrix[i][left])
        }
        left ++;
    }
    return ans
}
Spiral Matrix