<p>Given a positive integer <code>n</code>, generate an <code>n x n</code> <code>matrix</code> filled with elements from <code>1</code> to <code>n<sup>2</sup></code> in spiral order.</p>

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

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> [[1]]
</pre>

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

<ul>
	<li><code>1 &lt;= n &lt;= 20</code></li>
</ul>

class Solution {
public:
    vector<vector<int>> generateMatrix(int n) {
        int m = n;
        vector<vector<int>> ans(n,vector<int>(n,0));
        int top = 0, left = 0;
        int bottom = m-1, right = n-1;
        int count = 1;
        while(top <= bottom && left <= right) {
            for(int i = left; i<=right; i++) {
                ans[top][i] = count++;
            }
            top++;
            for(int i = top; i<=bottom; i++) {
                ans[i][right] = count++;
            }
            right--;
            if(top <= bottom) {
                for(int i = right; i>=left; i--) {
                    ans[bottom][i] = count++;
                }
                bottom--;
            }
            if(left <= right) {
                for(int i = bottom; i>=top; i--) {
                    ans[i][left] = count++;
                }
                left++;
            }
        }
        return ans;
    }
};

static const auto optimizeIO = []() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    return 0;
}();
8000