Can some help me where i went wrong?


#1

vector<vector > Solution::generateMatrix(int A) {
int t=0,d=A-1,l=0,r=A-1;
int v=1,dir=0;
int p=A*A;
vector<vector>res;
for(int i=0;i<A;i++){
for(int j=0;j<A;j++)res[i][j]=0;
}

while(t<=d&&l<=r){
    if(dir==0){
        for(int i=0;i<=r;i++){
            res[t][i]=v;
            v++;
        }
        dir++;
        t++;
    }
    if(dir==1){
        for(int i=t;i<=d;i++){
            res[i][r]=v;
            v++;
        }    
        dir++;
        r--;
    }
    if(dir==2){
        for(int i=r;i>=l;i--){
            res[d][i]=v;
            v++;
        }
        dir++;
        d--;
    }
    if(dir==3){
        for(int i=d;i<=t;i--){
            res[i][l]=v;
            v++;
        }
        dir-=3;
        l++;
    }
}
return res;

}


#2

For dir =0 part ,the i should start from l not from 0