简体   繁体   English

递归的困惑以及为什么我的列表没有保持更新?

[英]Confusion on Recursion and why my List isn't staying Updated?

import java.util.*;

public class MyClass {

    public static void main(String args[]) {

        int[] arr = new int[]{2,3,4,5};
        int[] min = new int[]{Integer.MAX_VALUE};
        int tgt = 7;
        List<Integer> lst = new ArrayList<>();
        List<Integer> sol = new ArrayList<>();
        
        recursion(arr, tgt, lst, min, sol);
        
        for (int i = 0; i < sol.size(); i++) {
            System.out.println(sol.get(i));
        }
      
    }
    
    public void recursion(int[] nums, int tgt, List<Integer> lst, int[] minLen, List<Integer> sol) {
        if (tgt < 0) {
            return;
        }
        if (tgt == 0) {
            if (lst.size() <= minLen[0]) {
                minLen[0] = lst.size();
            }
            sol = lst;
            return;
        }
        
        for (int i = 0; i < nums.length; i++) {
            List<Integer> cpy = new ArrayList<>(lst);
            cpy.add(nums[i]);
            recursion(nums, tgt - nums[i], cpy, minLen, sol);
        }
    }
}

The basic logic of what I'm trying to do is, given an array of numbers and a target, I want to return the smallest list of numbers that can be summed up to the target (in this case I'd want to return either {3,4} or {5,2}. I call a recursive function that breaks down my problem until I reach a base case in which I return or do some work on my sol list. lst is there to build up my current list in that recursive path while sol is there to update whenever I find a new path that is the new minimum length. However, what I think is happening is that when sol updates and returns, it becomes reset so sol is empty when it returns to main. I thought the sol list was being added to the Heap and would persist across all the calls when updated (like the minLen array)? Or am I missing something. What would be a way to get around this while maintaining my logic (I don't want to return a value but would rather have some data structure that just updates and that I could just r我正在尝试做的基本逻辑是,给定一个数字数组和一个目标,我想返回可以汇总到目标的最小数字列表(在这种情况下,我想返回{3,4} 或 {5,2}。我调用递归 function 来分解我的问题,直到我达到一个基本情况,在这种情况下我返回或在我的sol列表上做一些工作lst可以建立我当前的列表在那个递归路径中,而每当我找到一个新的最小长度的新路径时, sol就会在那里更新。但是,我认为正在发生的是,当sol更新并返回时,它会被重置,所以当它返回 main 时sol是空的.我认为sol列表被添加到堆中并且在更新时会在所有调用中持续存在(比如minLen数组)?或者我错过了什么。在保持我的逻辑的同时解决这个问题的方法是什么(我不'不想返回一个值,但宁愿有一些数据结构,只是更新,我可以只是 r eturn from my main function).从我的主要功能返回)。

When you have a code as follows:当您有如下代码时:

    public void recursion(int[] nums, int tgt, List<Integer> lst, int[] minLen, List<Integer> sol) {
        //... removed not relevant code
        sol = lst;

    }

What you are really doing is just assigning sol variable to point to a different object.您真正要做的只是将sol变量分配为指向不同的 object。 That doesn't have any affect on the sol variable that you passed as an argument in the main method.这对您在 main 方法中作为参数传递的sol变量没有任何影响。 That's because java passes arguments by value.那是因为 java 按值传递 arguments。 In this case value is a reference to the object.在这种情况下,值是对 object 的引用。

In the recursion method that reference is copied into sol variable (parameter).recursion方法中,引用被复制到sol变量(参数)中。 So, when you assign it a different value ( sol = lst ), you only update value of this one variable, that is local to the function.因此,当您为其分配一个不同的值 ( sol = lst ) 时,您只会更新这个变量的值,即 function 的本地变量。

You have two solutions here:您在这里有两个解决方案:

  1. Just add all values into a list that sol (passed as argument) points to.只需将所有值添加到sol (作为参数传递)指向的列表中。
  2. (better) just return result from within a function and assign it to new variable in main method. (更好)只需从 function 中返回结果并将其分配给 main 方法中的新变量。

It' usually a bad idea to modify objects passed into the function (unless that's the job of the function - like sort function).修改传递给 function 的对象通常是个坏主意(除非这是 function 的工作——比如sort函数)。 It's (almost) always better to return result with a return statement.使用return语句返回结果(几乎)总是更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM