【问题标题】:Storing all diagonals from top right to bottom left in array在数组中存储从右上角到左下角的所有对角线
【发布时间】:2021-02-21 15:18:16
【问题描述】:

我正在尝试将矩阵中从右上角到左下角的所有值存储在array 中。

    int matrixSample [][]  = {
            {6,4,1,4},
            {7,5,4,4},
            {4,4,8,3},
            {4,4,8,3}
            };

输出应该是

[4,1,4,4,4,3,6,5,8,3,7,4,8,4,4,4]

我可以得到右下角的对角线

static int[] getAllDiagonalsInMatrix(int matrix[][]){
    // Sum of arithmetic progression 
    int diagonal[] = new int[matrix.length * (matrix.length + 1)*2];
    int index = 0;  
    for(int row = 0; row < matrix.length; row++) {          
        for(int col = 0; col < matrix[row].length - row; col++) {
            diagonal[index++] = matrix[row + col][col];         
        }
    }
    return diagonal;
}

通过在上面的循环中进行调整,甚至可以使用相同的两个循环吗?

【问题讨论】:

  • 我认为您的输出与您的问题不匹配
  • 我已更改输出以匹配问题,感谢链接,这与我正在寻找的内容相似,但它从左上角开始。我会玩弄代码,看看能不能让我工作,谢谢

标签: java


【解决方案1】:

好的,这是我对您的问题的思考过程。但是,我将打印值而不是收集它们,以使我更轻松并保持解决方案易于阅读。

首先,如何获得对角线?我们需要经常这样做,所以让我们先为此创建一个函数。也许我们可以通过对角线的左上角,然后从那里走。

public void getDiagonal(int[][] array, int row, int col) {
    // While row and col are within the bounds of the array
    while (row < array.length && col < array[row].length) {
        // Print element in diagonal
        System.out.println(array[row][col]);

        // Diagonal moves from top-left to bottom-right
        row++;
        col++;
    }
}

现在我们有了一个获取对角线的函数,我们只需要一种方法来调用它。本质上,我们只需要遵循从右上角到左上角到左下角的 L 形即可。

// Get diagonals starting in the first row with a column > 0 
for (int col = array.length - 1; col > 0; col--) {
    getDiagonal(array, 0, col);
}

// Get all diagonals starting from the left most column
for (int row = 0; row < array.length; row++) {
    getDiagonal(array, row, 0);
}

现在我们有了一种迭代值的有效方法,我们可以重写它以将值保存到数组中。既然您有一个进程,您也可以选择完全删除该函数。

编辑:我差点忘了,但您要找的数学解如下。

for (int row = 0; row < array.length; row++) {
    for (int col = 0; col < array.length; col++) {
        // Index along diagonal
        int diagonal = Math.min(row, col);

        // Which part of L contains value
        if (col >= row) {
            int start = array.length - 1 - (col - row);
            int passed = start * (start + 1) / 2;
            solution[passed + diagonal] = array[row][col];
        } else {
            int start = array.length - 1 - (row - col);
            int passed = array.length * array.length - 1 - start * (start + 1) / 2;          solution[passed - array.length + 1 + row] = array[row][col];
        }
    }
}

【讨论】:

    【解决方案2】:

    一种解决方案是遍历一个矩阵,在该矩阵中考虑矩阵之外的位置,但排除每个索引超出范围。

    static int[] getDiagonals(int[][] mat) {
        int diagonal[] = new int[mat.length * (mat[0].length)];
        int index = 0;
        int yStart = -mat[0].length;
        for (int y = yStart; y < mat.length; y++) {
            for (int x = 0; x < mat[0].length; x++) {
                if (y + x >= 0 && y + x < mat.length) {
                    diagonal[index++] = mat[y+x][x];
                }
            }
        }
        return diagonal;
    }
    

    可能不是最优的,因为您有效地遍历了几乎两倍大小的矩阵,但它非常直观。

    【讨论】:

      猜你喜欢
      • 2021-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-21
      • 2022-06-25
      • 1970-01-01
      • 2019-07-10
      • 2014-05-17
      相关资源
      最近更新 更多