Solution of this problem in c++


#1

vector<vector > Solution::generateMatrix(int A)
{ int j,n,L,R,T,B;
vector<vector> v(A,vector (A));
L=0;
R=A-1;
T=0;
B=A-1;
n=1;
while(L<=R&&T<=B)
{

           for(j=L;j<=R;j++){v[T][j]=n;n++;}
           T++;
            
        
           if(T<B)
           {   
              for(j=T;j<=B;j++){v[j][R]=n;n++;}
              R--;
           }
           if(R>L)
           {
              for(j=R;j>=L;j--){v[B][j]=n;n++;}
              B--;
           }
           if(B>=T)
           {
              for(j=B;j>=T;j--){v[j][L]=n;n++;}
              L++;
           }
       
   }
return v;

}