【问题标题】:Convert negative index to positive index in an array (Trinomial Triangle)将数组中的负索引转换为正索引(三角三角)
【发布时间】:2020-05-31 13:28:16
【问题描述】:

我正在尝试查找三项式系数,并且我想避免在我的数组中使用负索引。在某些情况下 i 或 j 将变为负数并返回数组越界错误。无论如何我可以将包含在负索引中的数组镜像到正索引吗?

这是递归公式:Recursion Formula

我知道T(0, -1) = T(0, 1),但我该如何实现呢?

示例:

row 0: T(0, 0) = 1 , T(0, 1) = 0 ...

row 1: T(1, 0) = T(0, -1) + T(0, 0) + T(0, 1) , T(2, 0) ...

三项式系数T(n,k)x^(n+k)(1+x+x^2)^n的展开中的系数。

三项式三角形(中间索引为00左侧为负数,0右侧为正数):

注意:下面的代码从中间索引0向右遍历数组。

public class ClassNameHere {
    public static void main(String[] args) {
        int n = Integer.parseInt(args[0]);
        int k = Integer.parseInt(args[1]);
        long[][] DP = new long[n + 1][k + 1];
        DP[0][0] = 1;
        for (int i = 0; i <= n; i++) {
            for (int j = 0; j <= Math.min(i, k); j++) {
                if (i == j || ((j == 0) && i < 2)) DP[i][j] = 1;
                else if (j < -i) {
                    DP[i][j] = 0;
                } else DP[i][j] = DP[i - 1][j];
            }
        }
        System.out.println(DP[n][k]);
    }
}

现在我可以使用我的代码获取从T(0, 0)T(1, 0) 的条款,但无法通过添加T(1,0) + T(1, 1) + T(1, 2) 继续超过T(2, 0)。当我尝试实现DP[i][j] = DP[i - 1][j - 1] + DP[i - 1][j] + DP[i - 1][j + 1]时,它再次返回ArrayIndexOutOfBoundsException。我认为上述语句^的实现有问题。关于如何继续进行此操作的任何建议?

【问题讨论】:

    标签: java arrays multidimensional-array


    【解决方案1】:

    你可以这样填充这样一个数组:首先新建一个空数组,高n,宽2n+1,用零填充,并将上中点的第一个条目设置为1,然后迭代行和列并设置其他条目,每个条目等于其上方三个条目的总和:

    T[i][j] = T[i-1][j-1] + T[i-1][j] + T[i-1][j+1];
    

    代码:

    int n = 7;
    // new array filled with zeros
    int[][] arr = new int[n][2 * n + 1];
    // first entry
    arr[0][n] = 1;
    // iterate over the rows of the array
    // starting from the second
    for (int i = 1; i < arr.length; i++) {
        // iterate over the columns of the array
        // from the second to the penultimate
        for (int j = 1; j < arr[i].length - 1; j++) {
            // each entry is the sum of the three entries above it
            arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j] + arr[i - 1][j + 1];
        }
    }
    
    // output
    for (int[] row : arr) {
        for (int cell : row)
            if (cell > 0)
                System.out.printf("%3d ", cell);
            else
                System.out.print("    ");
        System.out.println();
    }
    

    输出:

                                  1                             
                              1   1   1                         
                          1   2   3   2   1                     
                      1   3   6   7   6   3   1                 
                  1   4  10  16  19  16  10   4   1             
              1   5  15  30  45  51  45  30  15   5   1         
          1   6  21  50  90 126 141 126  90  50  21   6   1     
    

    另见:Finding trinomial coefficients using dynamic programming

    【讨论】:

      【解决方案2】:

      我找到了原因。

      可以通过在二维数组中初始化正确的长度来完成。

      long[][] tri = new long[n + 1][k + n + 1];
      

      并使用 Math.abs() 处理 j 索引将流向负索引的实例。

      tri[i][j] = tri[i - 1][Math.abs(j - 1)] + tri[i - 1][j] + tri[i - 1][j + 1];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-01-31
        • 1970-01-01
        • 2011-03-29
        • 1970-01-01
        • 2015-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多