Given a positive integer n, generate an n x n matrix filled with elements from 1 to n2 in spiral order.

&nbsp;
Example 1:

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


Example 2:

Input: n = 1
Output: [[1]]


&nbsp;
Constraints:


	1 &lt;= n &lt;= 20


class Solution(object):
    def generateMatrix(self, n):
        if n==1:
            return [[1]]
        matrix=[[0 for a in range(n)] for b in range(n)]
        le_b=0
        u_b=0
        r_b=n-1
        lo_b=n-1
        ele=1
        while ele<(n**2)+1:
            i=u_b
            j=le_b
            while ele<(n**2)+1 and j<=r_b:
                matrix[i][j]=ele
                ele+=1
                j+=1
            u_b+=1
            i=u_b
            j=r_b
            while ele<(n**2)+1 and i<=lo_b:
                matrix[i][j]=ele
                ele+=1
                i+=1
            r_b-=1
            i=lo_b
            j=r_b
            while ele<(n**2)+1 and j>=le_b:
                matrix[i][j]=ele
                ele+=1
                j-=1
            lo_b-=1
            i=lo_b
            j=le_b
            while ele<(n**2)+1 and i>=u_b:
                matrix[i][j]=ele
                ele+=1
                i-=1
            le_b+=1
        return matrix
        ```
class Solution {
    public int[][] generateMatrix(int n) {
        int startingRow = 0;
        int endingRow = n-1;
        int startingCol = 0;
        int endingCol = n-1;

        int total = n*n;
        int element = 1;
        int[][] matrix = new int[n][n];

        while(element<=total){

        for(int i = startingCol; element<=total && i<=endingCol; i++){
            matrix[startingRow][i] = element;
            element++;
        }
        startingRow++;

        for(int i = startingRow; element<=total && i<=endingRow; i++){
            matrix[i][endingCol] = element;
            element++;
        }
        endingCol--;

        for(int i = endingCol; element<=total && i>=startingCol; i--){
            matrix[endingRow][i] = element;
            element++;
        }
        endingRow--;

        for(int i = endingRow; element<=total && i>=startingRow; i--){
            matrix[i][startingCol] = element;
            element++;
        }
        startingCol++;

        }

        return matrix;
    }
}
class Solution {
public:
    vector<vector<int>> generateMatrix(int n) {
        vector<vector<int>> vec( n , vector<int> (n, 0));

        vector<int>helper;
        for(int i=0;i<n*n;i++){
            helper.push_back(i+1);
        }

        int k = 0;
        int top = 0;
        int down = n-1;

        int left = 0;
        int right = n-1;

        int direction = 0;

        vector<int>result;

        while(top<=down and left<=right){
            if(direction==0){
                for(int i=left;i<=right;i++){
                    vec[top][i] = helper[k];
                    k++;
                }
                top++;
            }
            else if(direction==1){
                for(int i=top;i<=down;i++){
                    vec[i][right] = helper[k];
                    k++;
                }
                right--;
            }
            else if(direction==2){
                for(int i=right;i>=left;i--){
                    vec[down][i] = helper[k];
                    k++;
                }
                down--;
            }
            else if(direction==3){
                for(int i=down;i>=top;i--){
                    vec[i][left] = helper[k];
                    k++;
                }
                left++;
            }
            direction = (direction+1)%4;
        }
        return vec;
    }
};
var generateMatrix = function(n) {
    const arr = new Array(n).fill(0).map(() => new Array(n).fill(0));
    let count = 1, index = 1, i = 0, j =0, changed = false, toIncrease = true;
    arr[i][j] = index;
    while(index < n*n) {
        index++;
        if(i == n-count && j > count-1) {
            j--;
            toIncrease = false;
            changed = true;
        }
        else if(i !== n-count && j == n-count) {
            i++;
            toIncrease = false;
            changed = true;
        }
        if(i == count-1 && !changed) {
            if(toIncrease) j++;
            else j--;
        }
        else if(j == count-1 && !changed) {
            if(i == count) {
                toIncrease = true;
                j++;
                count++;
            }
            else if(toIncrease) i++;
            else i--;
        }
        arr[i][j] = index;
        if(index == 4*(n-1)) {
            toIncrease = true;
            count++;
        }
        changed = false;
    }
    return arr;
};
Spiral Matrix II