【问题标题】:ArrayList Recursion bheaviourArrayList 递归行为
【发布时间】:2021-08-09 01:32:41
【问题描述】:

我正在做一个问题,其中打印从 (0,0) 到 (row-1,cols-1) 的唯一路径.. 但是很难理解 arraylist 的行为。请解释这种行为以及正确的方法。

static void distnctpaths(int maze[][], int i, int j, int r, int c, ArrayList < Integer > path) {

    if (i == r && j == c) {
        path.add(maze[i][j]);
        System.out.println(path);
        return;
    }

    if (i == r + 1 || j == c + 1)
        return;

    path.add(maze[i][j]);

    distnctpaths(maze, i, j + 1, r, c, path);
    distnctpaths(maze, i + 1, j, r, c, path);


}

public static void main(String[] args) {
    int maze[][] = {
        { 1, 2, 3 },
        { 4, 5, 6 },
        { 7, 8, 9 },
    };
    ArrayList < Integer > path = new ArrayList < > ();

    distnctpaths(maze, 0, 0, 2, 2, path);
}

错误的输出

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

正确输出(供参考)

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

【问题讨论】:

    标签: java algorithm object recursion arraylist


    【解决方案1】:

    您的代码包含一个简单的逻辑错误。在路径中添加位置时,完成后不会将其删除。


    代码

    public static void distinctPaths(int[][] maze, int i, int j, int r , int c, ArrayList<Integer> path)
    {
        if(i == r && j == c)
        {
            path.add(maze[i][j]);
            System.out.println(path);
    
            path.remove(path.size()-1);
            return ;
        }
    
        if (i == r+1 || j == c+1) return;
    
        path.add(maze[i][j]);
        distinctPaths(maze,i,j+1, r, c, path);
        distinctPaths(maze, i+1, j, r,c,path);
        path.remove(path.size()-1);
    }
    

    改进

    上面多了一个path.add(index)path.remove(index)。下面是相同的代码,稍作修改,更简洁,消除了语句的重复。

    public static void distinctPaths(int[][] maze, int i, int j, int r , int c, ArrayList<Integer> path)
    {
        if (i == r+1 && j == c) System.out.println(path);
        if (i == r+1 || j == c+1) return;
    
        path.add(maze[i][j]);
    
        distinctPaths(maze,i,j+1, r, c, path);
        distinctPaths(maze, i+1, j, r,c,path);
    
        path.remove(path.size()-1);
    }
    

    它产生相同的输出,但它更短更好。


    输出

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

    这将解决您的问题。 我没有提供任何试运行来说明path.remove(index) 的意义是什么。您可以自己运行调试器来执行此操作。如果您仍然遇到任何问题,请发表评论。我会编辑我的答案来帮助你。

    【讨论】:

    • 感谢您提供如此详细而出色的答案。当我在函数调用 distinctPaths 中将路径作为新的 ArrayList(path) 传递时,我还有一个问题。它也给出了正确的答案,你能解释一下这个案例吗?
    • 是的,我可以。这里没有什么特别的事情发生。您正在使用以前的数组列表来构造一个新的数组列表。您正在使用数组列表的ArrayList(Collection&lt;? extends E&gt; c) 构造函数。这是官方 Javadoc 的链接:docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html
    • 如果你不知道generics,这看起来会很混乱。
    猜你喜欢
    • 2014-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-07
    • 2017-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多