# Print two-dimensional array of obliquely

• The result is as following:

``````1 3 6 10 15
2 5 9 14 19
4 8 13 18 22
7 12 17 21 24
11 16 20 23 25``````

• Here is my solution, it outputs [1, 2, 3, ... 23, 24, 25] in order, the code supports any size of rectangle.

``````public void zigzagPrint(int[][] M) {
int n = M.length, m = M[0].length;

// print upper left part
for (int r = 0; r < n; r++) {
int i = r, j = 0;
while (i >= 0 && j < m) {
System.out.format("%d ", M[i--][j++]);
}
}

// print lower right part
for (int c = 1; c < m; c++) {
int i = n - 1, j = c;
while (i >= 0 && j < m) {
System.out.format("%d ", M[i--][j++]);
}
}
}
``````

• ``````void Print(vector<vector<int>> v){
int m = v.size(), n = m ? v[0].size() : 0;
for(int j=0;j<n;j++){
int curj=j;
for(int i=0;i<m;i++){
if(curj<0) break;
cout<<v[i][curj--]<<" ";
}
}
for(int i=1;i<m;i++){
int curi=i;
for(int j=n-1;j>=0;j--){
if(curi>=m) break;
cout<<v[curi++][j]<<" ";
}
}
}``````

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