递归返回 ArrayIndexOutOfBound 异常

时间:2021-02-25 11:45:48

标签: java recursion

public class Check {

    static int count = 0;
    static final int totw = 4;

    public static int minh(int books[][], int bookno, int totw, int wl, int l) {
        if (bookno > books.length)
            return l;

        int sidebyside = 99;
        int top = 99;

        if (books[bookno][0] <= wl)
            sidebyside = minh(books, bookno + 1, totw, totw - (books[bookno][0] + wl), Math.max(l, books[bookno][1]));
        
        top = minh(books, bookno + 1, totw, totw - books[bookno][0], l + books[bookno][1]);

        return Math.min(sidebyside, top);
    }
    
    public static void main(String[] args) {
        int[][] books = { { 1, 1 }, { 2, 3 }, { 2, 3 } };

        System.out.println(minh(books, 1, totw, 3, 1));
    }
}

您能否帮助或纠正我实际上做错的地方?我用 99 来模拟一个非常大的数字。 我收到此错误:

<块引用>

线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: 3 在 Check.minh(Check.java:21) 在 Check.minh(Check.java:24) 在 Check.minh(Check.java:22) 在 Check.main(Check.java:37)

这是问题链接:https://leetcode.com/problems/filling-bookcase-shelves/

我知道这是非常低效的,但我只是在练习递归而已。

0 个答案:

没有答案