Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ArrayList克隆改进了运行时_Java_List_Arraylist_Clone - Fatal编程技术网

Java ArrayList克隆改进了运行时

Java ArrayList克隆改进了运行时,java,list,arraylist,clone,Java,List,Arraylist,Clone,问题是: 这是我的解决方案1: public class Solution { public List<List<Integer>> combine(int n, int k){ List<List<Integer>> result = new ArrayList<List<Integer>>(); combine(n, k, 1, result, new ArrayList&

问题是:

这是我的解决方案1:

   public class Solution {

    public List<List<Integer>> combine(int n, int k){
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        combine(n, k, 1, result, new ArrayList<Integer>());
        return result;
    }

    public void combine(int n, int k , int start, List<List<Integer>> result, ArrayList<Integer> l){
        if(k == 0){
            result.add(l);
            return;
        }
        for(int i = start; i <= n; ++i){

            l.add(i);
            combine(n, k - 1, i + 1, result, l);
        }
    }


    }
公共类解决方案{
公共列表合并(整数n,整数k){
列表结果=新建ArrayList();
合并(n,k,1,result,new ArrayList());
返回结果;
}
公共void合并(int n、int k、int start、列表结果、ArrayList l){
如果(k==0){
结果:添加(l);
返回;
}

对于(int i=start;i第一个解决方案确实不正确。请尝试调用
combine(5,3)
,并将其发送到
System.out
,您将看到第一个解决方案的输出为:

[1、2、2、3、3、3、4、4、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、5、[3,4,5,5,4,5,5],[1, 2, 3, 4, 5, 3, 4, 5, 4, 5, 5, 2, 3, 4, 5, 4, 5, 5, 3, 4, 5, 5, 4, 5, 5], [1, 2, 3, 4, 5, 3, 4, 5, 4, 5, 5, 2, 3, 4, 5, 4, 5, 5, 3, 4, 5, 5, 4, 5, 5], [1, 2, 3, 4, 5, 3, 4, 5, 4, 5, 5, 2, 3, 4, 5, 4, 5, 5, 3, 4, 5, 5, 4, 5, 5], [1, 2, 3, 4, 5, 3, 4, 5, 4, 5, 5, 2, 3, 4, 5, 4, 5, 5, 3, 4, 5, 5, 4, 5, 5], [1,2,3,4,5,3,4,5,4,5,5,2,3,4,5,4,5,5,5,5,4,5,5],[1,2,3,4,5,5,3,5,5,3,5,5,5,2,3,4,5,5,5,5,5,5,5,3,4,5,5,5]]

您会注意到,每个索引位置都是相同的列表-您确实需要每次创建一个新数组。对于第二个正确的解决方案,输出为:

[1,2,3]、[1,2,4]、[1,2,5]、[1,3,4]、[1,3,5]、[1,4,5]、[2,3,4]、[2,3,5]、[2,4,5]、[3,4,5]]


这意味着第一个解决方案的速度较慢,因为您每次都在向越来越大的列表中添加数字。对于更大的n和k值,该列表可能会非常大,并且在需要扩展时复制
ArrayList
的备份数组会成为一项非常昂贵的操作,这比复制/创建大量sm要昂贵得多所有列表。

如果您不打算修改解决方案,您可以使用共享链接列表做得更好。他如何修复第一个解决方案?他做错了什么?谢谢。@Unheilig解决方案是每次创建一个新数组,这是他在第二个解决方案中所做的。
public class Solution {

public List<List<Integer>> combine(int n, int k){
    List<List<Integer>> result = new ArrayList<List<Integer>>();
    combine(n, k, 1, result, new ArrayList<Integer>());
    return result;
}

public void combine(int n, int k , int start, List<List<Integer>> result, ArrayList<Integer> l){
    if(k == 0){
        result.add(l);
        return;
    }
    for(int i = start; i <= n; ++i){
        ArrayList<Integer> a = (ArrayList<Integer>) l.clone();
        a.add(i);
        combine(n, k - 1, i + 1, result, a);
    }
}


}