http://algorithms.tutorialhorizon.com/print-all-diagonals-of-a-given-matrix/
Google Interview - Print Matrix Diagonally - 我的博客 - ITeye技术网站
Given a 2D matrix, print all elements of the given matrix in diagonal order.
http://www.ideserve.co.in/learn/print-matrix-diagonally
Given a matrix of mxn dimensions, print the elements of the matrix in diagonal order.
Then, number of diagonals will be = rowCount + columnCount - 1 as depicted in the diagram below.
http://www.geeksforgeeks.org/print-matrix-diagonal-pattern/
Read full article from Google Interview - Print Matrix Diagonally - 我的博客 - ITeye技术网站
We will solve this problem in two parts. first half of diagonals and second half of diagonals.
public static void print(int [][] a){
//print first half
int row =0;
int col;
while(row<a.length){
col =0;
int rowTemp = row;
while(rowTemp>=0){
System.out.print(a[rowTemp][col] + " ");
rowTemp--;
col++;
}
System.out.println();
row++;
}
//print second half
col = 1;
while(col<a.length){
int colTemp = col;
row = a.length-1;
while(colTemp<=a.length-1){
System.out.print(a[row][colTemp] + " ");
row--;
colTemp++;
}
System.out.println();
col++;
}
}
Google Interview - Print Matrix Diagonally - 我的博客 - ITeye技术网站
Given a 2D matrix, print all elements of the given matrix in diagonal order.
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 18 19 20
Diagonal printing of the above matrix is
1
5 2
9 6 3
13 10 7 4
17 14 11 8
18 15 12
19 16
20
- public void printDiagonal(int[][] A) {
- int m = A.length, n = A[0].length;
- for (int i = 0; i < m+n-1; i++) {
- int row = Math.min(i, m-1);
- int col = Math.max(0, i-m+1);
- while(row>=0 && col<n) {
- System.out.print(A[row--][col++] + " ");
- }
- System.out.println();
- }
- }
总共需要打印的层数,是长度加宽度减去一。关键在于内层的
row = i - j,而col = j。private static void printDiagonal(int[][] matrix){
int m = matrix.length;
int n = matrix[0].length;
// 计算打印的层数
int lvl = m + n - 1;
for(int i = 0; i < lvl; i++){
for(int j = i; j >= 0; j--){
int row = i - j;
int col = j;
// 超过边界的点直接跳过
if(row >= m || col >= n) continue;
System.out.print(matrix[row][col]+" ");
}
System.out.println();
}
}
Given a matrix of mxn dimensions, print the elements of the matrix in diagonal order.
Then, number of diagonals will be = rowCount + columnCount - 1 as depicted in the diagram below.

public static void printMatrixDiagonally(int[][] matrix) { int row, col; int rowCount = matrix.length; int columnCount = matrix[0].length; for (int k = 0; k < rowCount; k++) { for (row = k, col = 0; row >= 0 && col < columnCount; row--, col++) { System.out.print(matrix[row][col] + " "); } System.out.println(); } for (int k = 1; k < columnCount; k++) { for (row = rowCount - 1, col = k; row >= 0 && col < columnCount; row--, col++) { System.out.print(matrix[row][col] + " "); } System.out.println(); } }
Given a matrix of n*n size, the task is to print its elements in diagonally pattern.
- We use a flag isUp to decide whether we need to go upward direction or downward direction. We set isUp = true initially that first we are going upward.
- If isUp = 1 then start printing elements by incrementing column index and decrementing the row index.
- Similarly if isUp = 0, then decrement the column index and increment the row index.
- Do this till all the elements get traversed.
void printMatrixDiagonal(int mat[MAX][MAX], int n){ // Initialize indexes of element to be printed next int i = 0, j = 0 ; // Direction is initially from down to up bool isUp = true; // Traverse the matrix till all elements get traversed for (int k=0; k<n*n;) { // If isUp = true then traverse from downward // to upward if (isUp) { for ( ; i>=0 && j<n ; j++, i--) { cout << mat[i][j] << " "; k++; } // Set i and j according to direction if (i < 0 && j<=n-1) i = 0; if (j == n) i = i+2 , j--; } // If isUp = 0 then traverse up to down else { for ( ; j >= 0 && i<n ; i++ , j--) { cout << mat[i][j] << " "; k++; } // Set i and j according to direction if (j < 0 && i<=n-1) j = 0; if (i == n) j = j + 2 , i--; } // Revert the isUp to change the direction isUp = !isUp; }}