带有2d数组的递归方法(java)

时间:2013-01-10 07:04:53

标签: java recursion

我必须写一个方法, 该方法接收二维整数数组的参数。该方法返回具有最高整数总和的行号。我只允许使用递归!没有循环允许! - 当然我需要创建一个私有方法,将一行汇总为一个数组,然后我必须做另一个比较行的私有方法,但它不会真正起作用,因为它我写的方法只适用于1d数组,我需要比较2d数组中的一行..

欣赏各种帮助..

我的一些代码:

private  int rowSum(int[] array, int index) {//the sum of an array(1d array)
        if (index == array.length)
            return 0;
        else
            return array[index] + rowSum(array, index + 1);
    }

**public int maxRow(int[][] a){------!!!---the problem...

    }**

5 个答案:

答案 0 :(得分:1)

我会尝试使用参数调用main的递归方法:

  • 阵列[] []
  • 当前列索引
  • 当前行索引
  • 当前行总和
  • 最高行索引
  • 最高行和

我在这里工作,所以这绝对是可能的。 这是大约15行代码。 祝你好运!

答案 1 :(得分:0)

作为入门练习,请尝试将rowSum功能的签名更改为:

int rowSum(int[] array, int index, int sumSoFar)

您应该能够编写函数,使得递归部分的返回表达式只是一个递归调用:

if (...) // exit case
  return sumSoFar;
else
  return rowSum(...);

(现在这是一个尾递归实现。)

有了这种心态,想想你会怎么写:

int indexOfMaxValue(int[] array, int index, int maxValueSoFar, int maxIndexSoFar)

一旦得到这些,您应该能够将这两个概念结合起来。

答案 2 :(得分:0)

以下代码已准备好运行并测试:

public class RecursionSum {
    /* Sum a row until <code>index</code>. */
    private static int rowSum(int[] a, int index) {
        if (index == 0) { return a[0]; }
        // add the current element to the recursive sum
        return a[index] + rowSum(a, index - 1);
    }

    /* Sum a whole row. */
    private static int rowSum(int[] a) {
        return (a.length == 0) ? 0 : rowSum(a, a.length - 1);
    }

    /* Find the index of the array having the max sum until <code>index</code>. */
    private static int maxRow(int[][] a, int index){
        if (index == 0) { return 0; }
        // compare the current row's sum with the recursive max row's sum,
        // update index when it's a new winner
        int maxIndex = maxRow(a, index - 1);
        return (rowSum(a[maxIndex]) < rowSum(a[index])) ? index : maxIndex;
    }

    /* Find the index of the array having the max sum. */
    public static int maxRow(int[][] a){
        return a.length == 0 ? 0 : maxRow(a, a.length - 1);
    }

    /* very simple test */
    public static void main(String[] args) {
        int a1[][] = {};
        int a2[][] = {{1, 2, 3, 4}};
        int a3[][] = {{ 1, 2, 3, 4}, {8, 90}, {5, 6, 7}, {300, 4, 9}, {4, 6, 12}};
        System.out.println(maxRow(a1));
        System.out.println(maxRow(a2));
        System.out.println(maxRow(a3));
    }
}

答案 3 :(得分:0)

代码:

public class MainClass {
    static  int contents[][] = { {1, 2 , 3, 4} , { 4, 5, 8, 7}, { 4, 2, 8, 7}, { 4, 5, 0, 7} };
    public static void main(String[] args) 
    {
        System.out.println(getIndexOfRowWithHighestSum(contents, 0, 0));
    }
    public static int getIndexOfRowWithHighestSum(int[][] twoDAray, int currentIndex,int indexWithHighestSum){
        if(currentIndex>=twoDAray.length){
            return indexWithHighestSum;
        }
        int sum1 = getSumOfArray(twoDAray[currentIndex], 0) ;
        int sum2 = getSumOfArray(twoDAray[indexWithHighestSum], 0);

        indexWithHighestSum = (sum1 > sum2)?currentIndex:indexWithHighestSum;

        return getIndexOfRowWithHighestSum(twoDAray, currentIndex+1,indexWithHighestSum);
    }
    public static int getSumOfArray(int[] array, int currentIndex){
        if(currentIndex>=array.length){
            return 0;
        }
        return array[currentIndex]+getSumOfArray(array,currentIndex+1);
    }
}

答案 4 :(得分:0)

这是我想出来的……

public static void main(String[] args)
{
    int[][] mat = { { 2, 6, 5, 4 }, { 3, 17, 2, 6 }, { 1, 3, 21, 0 } };

    System.out.println(maxRow(mat)); // 1

    int[][] mat1 = { { 2, 6, 5, 4 }, { 1, 3, 21, 0 }, { 3, 17, 2, 6 } };

    System.out.println(maxRow(mat1)); // 2
}

public static int maxRow(int[][] mat)
{
    return maxRow(mat, 0, 0, -1);
}

private static int maxRow(int[][] mat, int row, int maxSumInRow, int maxRowNo)
{
    if (row == mat.length)
        return maxRowNo;
            
    int sumRow = sumRow(mat, row, 0);
    
    if (sumRow > maxSumInRow)
    {
        maxSumInRow = sumRow;
        maxRowNo = row;
    }

    return maxRow(mat, row + 1, maxSumInRow, maxRowNo);
}

private static int sumRow(int[][] mat, int row, int col)
{
    if (col == mat[row].length)
        return 0;
    
    return mat[row][col] + sumRow(mat, row, col + 1);
}