package leetcode;

import java.util.Arrays;

/**
 * https://leetcode.com/problems/diagonal-traverse/
 */
public class Problem498 {
    public int[] findDiagonalOrder(int[][] matrix) {
        // TODO
        return new int[0];
    }

    public static void main(String[] args) {
        Problem498 prob = new Problem498();
        System.out.println(Arrays.toString(prob.findDiagonalOrder(new int[][]{
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}
        }))); // [1,2,4,7,5,3,6,8,9]
        System.out.println(Arrays.toString(prob.findDiagonalOrder(new int[][]{
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12}
        }))); // [1,2,5,9,6,3,4,7,10,11,8,12]
    }
}

@@ -16,34 +16,8 @@
 e;%0A%0A
-import java.util.Arrays;%0A%0A
 /**%0A
@@ -163,217 +163,710 @@
     
-// TODO%0A        return new int%5B0%5D;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem498 prob = new Problem498();%0A        System.out.println(Arrays.toString(prob.findDiagonalOrder(new int%5B%5D%5B%5D%7B
+int maxRow = matrix.length;%0A        if (maxRow == 0) %7B%0A            return new int%5B0%5D;%0A        %7D%0A        int maxCol = matrix%5B0%5D.length;%0A        int%5B%5D result = new int%5BmaxRow * maxCol%5D;%0A        boolean up = false;%0A        int idx = 0;%0A        int row = 0;%0A        int col = 0;%0A        result%5Bidx++%5D = matrix%5Brow%5D%5Bcol%5D;%0A        while (idx %3C maxRow * maxCol) %7B%0A            if (up) %7B%0A                if (row + 1 %3C maxRow) %7B%0A                    row++;%0A                %7D else %7B%0A                    col++;%0A                %7D%0A                while (0 %3C= row && row %3C maxRow && 0 %3C= col && col %3C maxCol) %7B%0A                    result%5Bidx++%5D = matrix%5Brow%5D%5Bcol%5D;%0A                    row--;%0A                    col++;
 %0A   
@@ -878,18 +878,13 @@
     
-%7B1, 2, 3%7D,
+    %7D
 %0A   
@@ -896,18 +896,18 @@
     
-%7B4, 5, 6%7D,
+    row++;
 %0A   
@@ -919,259 +919,504 @@
     
-%7B7, 8, 9%7D%0A        %7D))); // %5B1,2,4,7,5,3,6,8,9%5D%0A        System.out.println(Arrays.toString(prob.findDiagonalOrder(new int%5B%5D%5B%5D%7B%0A            %7B1, 2, 3, 4%7D,%0A            %7B5, 6, 7, 8%7D,%0A            %7B9, 10, 11, 12%7D%0A        %7D))); // %5B1,2,5,9,6,3,4,7,10,11,8,12%5D
+    col--;%0A            %7D else %7B // down%0A                if (col + 1 %3C maxCol) %7B%0A                    col++;%0A                %7D else %7B%0A                    row++;%0A                %7D%0A                while (0 %3C= row && row %3C maxRow && 0 %3C= col && col %3C maxCol) %7B%0A                    result%5Bidx++%5D = matrix%5Brow%5D%5Bcol%5D;%0A                    row++;%0A                    col--;%0A                %7D%0A                row--;%0A                col++;%0A            %7D%0A            up = !up;%0A        %7D%0A        return result;
 %0A   

Solve problem 498