Print a matrix anti diagonally


  • 0
    S

    Input :

    [ [ 1  2  3], 
      [ 4  5  6],
      [ 7  8  9],
      [10 11 12]]
    

    Output - 1 2 4 3 5 7 6 8 10 9 11 12


  • 0
    R
    void Print(vector<vector<int>> A){
        int m=A.size(), n=A[0].size();
        for(int i=0;i<=m+n-2;i++){
            for(int j=0;j<=i;j++){
                if(j>=m || i-j>=n) continue;
                int k=i-j;
                cout<<A[j][k]<<" ";
            }
        }
    }

  • 0
    G

    nice solution! how to figure this out though, thx!


  • 0
    R

    I call this as enumeration problem, coding skills. You should find the diffenrent anti diagonal line features, and for each anti diagonal line, finding the different feature. Also I thinks everywhere A[][] should be some restrictions, which make some inequally, this always making bugs. I mean if(j>=m || i-j>=n) continue; here


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.