<p>Given an <code>m x n</code> <code>matrix</code>, return <em>all elements of the</em> <code>matrix</code> <em>in spiral order</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/13/spiral1.jpg" style="width: 242px; height: 242px;" />
<pre>
<strong>Input:</strong> matrix = [[1,2,3],[4,5,6],[7,8,9]]
<strong>Output:</strong> [1,2,3,6,9,8,7,4,5]
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/13/spiral.jpg" style="width: 322px; height: 242px;" />
<pre>
<strong>Input:</strong> matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
<strong>Output:</strong> [1,2,3,4,8,12,11,10,9,5,6,7]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>m == matrix.length</code></li>
	<li><code>n == matrix[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 10</code></li>
	<li><code>-100 &lt;= matrix[i][j] &lt;= 100</code></li>
</ul>

class Solution {
public:
    vector<int> spiralOrder(vector<vector<int>>& matrix) {
        int row=matrix.size();
        int col=matrix[0].size();
        int count=0;
        int starting_row=0;
        int starting_col=0;
        int ending_row=row-1;
        int ending_col=col-1;
        vector<int>ans;
        int total=row*col;
        while(count<total){
            for(int i=starting_row;count<total && i<=ending_col;i++){
                ans.push_back(matrix[starting_row][i]);
                count++;
            }
            starting_row++;
            for(int i=starting_row;count<total && i<=ending_row;i++){
                ans.push_back(matrix[i][ending_col]);
                count++;
            }
            ending_col--;
            for(int i=ending_col;count<total && i>=starting_col;i--){
                ans.push_back(matrix[ending_row][i]);
                count++;
            }
            ending_row--;
            for(int i=ending_row;count<total && i>=starting_row;i--){
                ans.push_back(matrix[i][starting_col]);
                count++;
            }
            starting_col++;
        }
        return ans;
    }
};
8200