Getting runtime error. Can anyone help me out?


#1

vector<vector > Solution::diagonal(vector<vector > &A) {

int s = 0 ;
vector < vector <int> > ans ;
int r , c ;

int n = A.size();
 while ( s < 2*n -1){
     while( s < n ){
        r = 0 ;
        c = s ;
        while(r < n && c > 0){
            ans[s].push_back(A[r][c]);
            r++ ;
            c-- ;
            
        }
        s++ ;
        if(s == n)
        break;
     }
     
     while( s < 2*n-1 && s >= n){
        r = s-n+1 ;
        c = n-1 ;
        while(r < n && c > 0){
            ans[s].push_back(A[r][c]);
            r++ ;
            c-- ;
            
        }
        s++ ;
        if(s == 2*n-1)
        break ;
     }
     
     
 }
 
 return ans ;

}


#2

define size of vector ans. i.e. vector<vector > ans(2*n-1)


#3

It worked ! Thank you