Traverse Matrix in Diagonal strips

前端 未结 16 1857
暖寄归人
暖寄归人 2020-11-28 02:55

I thought this problem had a trivial solution, couple of for loops and some fancy counters, but apparently it is rather more complicated.

So my question is, how woul

16条回答
  •  鱼传尺愫
    2020-11-28 03:46

    public void printMatrix(int[][] matrix) {
        int m = matrix.length, n = matrix[0].length;
        for (int i = 0; i < m + n - 1; i++) {
             int start_row = i < m ? i : m - 1;
             int start_col = i < m ? 0 : i - m + 1;
             while (start_row >= 0 && start_col < n) {
                   System.out.print(matrix[start_row--][start_col++]);
             }
             System.out.println("\n")
         }
    }
    

提交回复
热议问题