`

Google Interview - Print Matrix Diagonally

 
阅读更多

Given a 2D matrix, print all elements of the given matrix in diagonal order. For example, consider the following 5 X 4 input matrix.

    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();
    }
}

  

分享到:
评论

相关推荐

    非负矩阵分解

    非负矩阵分解,可用于文本的话题挖掘.Non-negative matrix factorization (NMF) has previously been shown to be a useful decomposition for multivariate data. Two different multiplicative algorithms for NMF...

    chess象棋开发最大最小值法.zip

    已注释

    WPF 国际象棋 棋子 ChessProgrammingTest.zip

    WPF 国际象棋 棋子ChessProgrammingTest.zip 要求: You have been provided with a third-party library ...* Queen – Moves diagonally, horizontally or vertically, any distance within board boundaries

    Gauss-Seidel-Method.zip_To the Letter

    Though it can be applied to any matrix with non-zero elements on the diagonals, convergence is only guaranteed if the matrix is either diagonally dominant, or symmetric and positive definite....

    FinalCAS_LC860EQY-FJA1_General_ver1.1_v0.1.pdf

    It has a 85.60 inch diagonally measured active display area with QWUXGA resolution (2160 vertical by 3840 horizontal pixel array). Each pixel is divided into Red, Green and Blue sub-pixels or dots ...

    gauss_seidel.rar_To the Letter

    Though it can be applied to any matrix with non-zero elements on the diagonals, convergence is only guaranteed if the matrix is either diagonally dominant, or symmetric and positive definite....

    LD470DUN.pdf

    It has a 46.96 inch diagonally measured active display area with WUXGA resolution (1080 vertical by 1920 horizontal pixel array). Each pixel is divided into Red, Green and Blue sub-pixels or dots ...

    right_side_left.tar.gz_Upside Down_lena

    upside-down lena.im right-side-left lena.im diagonally mirrored lena.im

    Gauss-Seidel-method.zip_After Method_liebmann

    Though it can be applied to any matrix with non-zero elements on the diagonals, convergence is only guaranteed if the matrix is either diagonally dominant, or symmetric and positive definite.

    H-H101D-27C(HSD 40pin排线,3.0mm厚度)Spec

    HONGCAI Display model H-H101D-27C (For HannStar LCD) is a color active matrix thin film transistor(TFT) liquid crystal display (LCD) that uses amorphous silicon TFT as a switching device. This model ...

    The Triangle

    Each step can go either diagonally down to the left or diagonally down to the right. 输入 Your program is to read from standard input. The first line contains one integer N: the number of rows in ...

    The triangle

    Each step can go either diagonally down to the left or diagonally down to the right. Input Your program is to read from standard input. The first line contains one integer N: the number of rows in ...

    经典算法题-矩阵中查找单词路径数

    1.问题描述 Problem Statement 问题陈述 You are given a String[] grid representing a rectangular grid of letters.... The path may move up, down, left, right, or diagonally from one letter to

    对角占优矩阵在偏微分方程中的应用 (2014年)

    主要讨论对角占优及严格对角占优(strictly diagonally dominant)矩阵的相关引理和定理,以及在偏微分方程边值问题中的应用(指数衰减因子)。

Global site tag (gtag.js) - Google Analytics