C++ solution using for loops


#1

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

 int c = 1;
 
 for(int i =0; i<A; i++) {
     for(int j =i; j<A-i;j++){v[i][j]=c;c++;}
     for(int j =i+1; j<A-i;j++){v[j][A-i-1]=c;c++;}
     for(int j =A-i-2;j>=i;j--){v[A-i-1][j]=c;c++;}
     for(int j =A-i-2;j>i;j--){v[j][i]=c;c++;}
 }
 return v;

}