Simple C++ using basic recursion


#1
void solve(vector<vector<int>>&mat,int num,int A,int start){
    if(A<1){
        return ;
    }
    for(int i = start ; i<A;i++){
        mat[start][i] = num+1;
        num++;
    }
    for(int i = start+1 ; i<A ; i++){
        mat[i][A-1] = num+1;
        num++;
    }
    for(int i = A-2;i>=start;i--){
        mat[A-1][i] = num+1 ;
        num++;
    }
    for(int i = A-2 ; i>start;i--){
        mat[i][start] = num+1 ;
        num++;
    }
    solve(mat,num,A-1,start+1);
    return ;
    
}
vector<vector<int> > Solution::generateMatrix(int A) {
    vector<vector<int>> mat(A,vector<int>(A,0)) ;
    solve(mat,0,A,0);
    return mat ;
    
}