Java very easy to understand solution

  • 0
    public int[] findDiagonalOrder(int[][] matrix) {
        if(matrix==null || matrix.length==0) return new int[0];
        int rowend = matrix.length-1;
        int colend = matrix[0].length-1;
        int[] res = new int[(rowend+1) * (colend+1)];
        int idx=0, layer=0;
            //traverse up
            for(int i=Math.min(rowend, layer); i>=0; i--){
                int j = layer - i;
                if(j>colend) break;
                res[idx++] = matrix[i][j];
            layer ++;
            //traverse down
            for(int j=Math.min(colend, layer); j>=0; j--){
                int i = layer - j;
                if(i> rowend) break;
                res[idx++] = matrix[i][j];
            layer ++;
        return res;

Log in to reply

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