查找哪个矩阵行具有最高和java

我正在尝试编写一个递归非静态方法,该方法对于每个给定矩阵将返回具有最高总和的行号。我不允许使用静态方法和 for 循环(在我编写的任何方法中)。

我认为解决方案是使用三种方法:

  1. (private) 计算给定行的总和。

  2. (私人)比较行的总和与使用方法#1i的总和i+1

  3. (公共)检查矩阵是否有多于一行并在行上应用#2 0

我觉得我用我解决这个问题的方式把事情复杂化了。如果有人愿意向我建议更好的算法,我会很乐意尝试。

无论如何,我相信我对#1 和#3 没问题。我的问题是#2。我不知道如何设置行号变量:

public class Matrix {


private int[][] _mat;


public Matrix(int sizeRow, int sizeCol) {

   _mat = new int[sizeRow][sizeCol];

}


private int maxRow(int row) { //Recursive method #2: comparing sum of i and i+1

        int rowNumber;

        if (row <= _mat.length) {

            if (rowSum(row, 0) > rowSum(row+1,0)) {

                rowNumber = row;

                return maxRow(row+1);

            }

            else {

                return rowNumber;

            }

        }

        else {

            return rowNumber;

        }

    }

..

public int maxRow() {..} //Recursive method #3


private int rowSum(int i, int j) {..} //Recursive method #1


}

我的问题是 var rowNumber。它还没有被初始化,如果我要初始化它,它将被设置为0每次我调用该方法时。


临摹微笑
浏览 154回答 2
2回答

斯蒂芬大帝

如果需要返回总和最大的行的索引,可以这样做:private int maxRow(int current_index, int max_index) {&nbsp; &nbsp; if (current_index == _mat.length) {&nbsp; &nbsp; &nbsp; &nbsp; return max_index;&nbsp; &nbsp; } else if (sumRow(current_index) > sumRow(max_index)) {&nbsp; &nbsp; &nbsp; &nbsp; return maxRow(current_index+1, current_index);&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; return maxRow(current_index+1, max_index);&nbsp; &nbsp; }}maxRow(1, 0); //method call第一个参数current_index存储您当前正在测试的索引,而参数max_index存储到目前为止访问过的具有最大总和的索引。第一个子句确保您在到达数组末尾时返回具有最大总和值的任何索引。max_index一旦找到总和高于之前的行,第二个子句就会更新。当上述情况没有发生时,第三个子句只是迭代到下一行。您可以调用该方法current_index=1,max_index=0因此您不需要max_index使用无效值进行初始化。如果你想提高性能,你还可以添加一个新的参数max_value来存储当前的最大和,这样你就不需要在每次递归调用时都调用它。

慕田峪7331174

这是一个示例,说明如何检索总和最高的行的索引。public class MaxRowFromArray {&nbsp; &nbsp; private final int[][] values;&nbsp; &nbsp; public MaxRowFromArray(int[][] values) {&nbsp; &nbsp; &nbsp; &nbsp; this.values = values;&nbsp; &nbsp; }&nbsp; &nbsp; private int sumOfRow(int[] row, int rowIndex, int sum) {&nbsp; &nbsp; &nbsp; &nbsp; if (rowIndex > row.length - 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return sum;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return sumOfRow(row, rowIndex + 1, sum + row[rowIndex]);&nbsp; &nbsp; }&nbsp; &nbsp; private int highestRow(int column, int highestIndex, int highestRow) {&nbsp; &nbsp; &nbsp; &nbsp; if (column > values.length - 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return highestIndex;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; int sumOfRow = sumOfRow(values[column], 0, 0);&nbsp; &nbsp; &nbsp; &nbsp; if (sumOfRow > highestRow) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return highestRow(column + 1, column, sumOfRow);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return highestRow(column + 1, highestIndex, highestRow);&nbsp; &nbsp; }&nbsp; &nbsp; public int highestRow() {&nbsp; &nbsp; &nbsp; &nbsp; int highest = highestRow(0, 0, -1);&nbsp; &nbsp; &nbsp; &nbsp; if (highest == -1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new IllegalStateException("No row can be found with the highest sum.");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return highest;&nbsp; &nbsp; }}测试&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; MaxRowFromArray max = new MaxRowFromArray(new int[][] {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { 1 },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { 1, 2 },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { 1, 2, 3 },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { 1, 2, 3, 4}&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; int expectedHighest = 3;&nbsp; &nbsp; &nbsp; &nbsp; int highestIndex = max.highestRow();&nbsp; &nbsp; &nbsp; &nbsp; if (highestIndex != expectedHighest) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new AssertionError(String.format("Highest index %s was not the expected highest %s.",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; highestIndex, expectedHighest));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Highest: " + highestIndex);&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java