Easy C++ O(A*A) solution


#1

vector<vector > Solution::generateMatrix(int ans) {
vector<vector >A(ans,vector(ans,0));

    int count=1;
    int starti=0;//
    int startj=0;//
    int endi=ans-1;
    int endj=ans-1;

while(starti<=endi)
{
   
    
    for(int j=startj;j<=endj;j++)
    {
        A[starti][j]=count;
        count++;
    }
    for(int i=starti+1;i<=endi;i++)
    {
        A[i][endj]=count;
        count++;
    }
    for(int j=endj-1;j>=startj;j--)
    {
        A[endi][j]=count;
        count++;
        
    }
    for(int i=endi-1;i>=starti+1;i--)
    {
        A[i][startj]=count;
        count++;
    }
    starti++;
    startj++;
    endi--;
    endj--;
}
return A;

}