Easy C++/CPP solution


#1

vector<vector > Solution::generateMatrix(int A) {
int k = 1, rs = 0, re = A - 1, cs = 0, ce = A - 1, square = A * A;
int ans=1;
vector<vector > res(A,vector(A, 0));
while(k <=square){
for(int i= rs ; i<=ce ; i++ ) res[rs][i] = k++;
rs++;
for(int i = rs; i<= re ;i++) res[i][ce] = k++;
ce–;
for(int i =ce;i>=cs;i–) res[re][i] = k++;
re–;
for(int i=re;i>=rs;i–) res[i][cs] = k++;
cs++;
}
return res;
}