<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:
    def generateMatrix(self, n: int) -> List[List[int]]:
        # Generate empty matrix
        matrix = []
        for i in range(n):
            matrix.append([0] * n)
        
        current_number = 1 
        left, right = 0, n - 1
        top, bottom = 0, n - 1
        
        while (left <= right and top <= bottom):
            for i in range(left, right + 1):
                matrix[top][i] = current_number
                current_number += 1
            top += 1
            
            for i in range(top, bottom + 1):
                matrix[i][right] = current_number
                current_number += 1
            right -= 1
            
            if top <= bottom:
                for i in range(right,left - 1,-1):
                    matrix[bottom][i] = current_number
                    current_number += 1
                bottom -= 1
            
            if left <= right:
                for i in range(bottom,top - 1,-1):
                    matrix[i][left] = current_number
                    current_number += 1
                left += 1
        
        return matrix
16300