【发布时间】:2019-02-23 05:47:54
【问题描述】:
下面是我的函数,它给出了给定数组中的元素总和到特定目标的所有可能性。我可以打印列表,但是结果列表没有更新。
public List<List<Integer>> helper(List<List<Integer>> res, int[] c, int l, int h, int target, List<Integer> temp){
if(target == 0){
res.add(temp);
System.out.println(temp);
return res;
}
if(target < c[l]){
return res;
}
for(int i = l; i <=h; i++){
temp.add(c[i]);
res = helper(res, c,i,h,target-c[i], temp);
temp.remove(temp.size()-1);
}
return res;
}
res 最后是空数组列表的数组列表,但第 5 行正确打印了临时数组列表。
函数调用如下。
List<List<Integer>> res = new ArrayList<List<Integer>>();
List<Integer> temp = new ArrayList<Integer>();
res = helper(res,candidates, 0, candidates.length-1, target, temp);
示例: 给定数组 = [1,2,3],目标 = 6
标准输出:
[1, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 2]
[1, 1, 1, 3]
[1, 1, 2, 2]
[1, 2, 3]
[2, 2, 2]
[3, 3]
res is [[],[],[],[],[],[],[]]
【问题讨论】:
-
你的输入是什么,你的调试结果是什么?
-
更新了输入和结果