【发布时间】:2020-10-22 10:06:27
【问题描述】:
我已经编写了一些代码来尝试解决这个挑战,但它不起作用,我似乎无法弄清楚哪里出错了,我可以在网上找到答案,但这不是我想要看到的重点为什么我的代码不起作用。
问题:
给定一组候选编号(candidates)(不重复)和一个目标编号(target),找出候选编号总和为target的所有唯一组合。
可以从候选人中无限次选择相同的重复数字。
Input: candidates = [2,3,6,7], target = 7,
A solution set is:
[
[7],
[2,2,3]
]
这是我想出的:
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList<>();
helper(res,new ArrayList<Integer>(), candidates,target,0,0);
return res;
}
//current = current sum, we want it to be target
//start is index we are at and where the for loop starts
public void helper(List<List<Integer>> res, List<Integer> temp, int[] nums, int target, int current, int start){
if(start>=nums.length){
return;
}
if(current>=target){
if(current==target){
res.add(new ArrayList<>(temp));
}
temp.remove(temp.size()-1);
helper(res,temp,nums,target,current-nums[start],start+1);
return;
}
for(int i=start; i<nums.length; i++){
temp.add(nums[i]);
helper(res,temp,nums,target,current+nums[i],start);
}
}
}
我的代码解释:
所以我在这里尝试使用递归回溯。我一直循环数组中的一个元素,直到总和 >= target。如果它的 >target 我删除了最后一个元素,因为它使它大于 target 并尝试其他元素。如果它的 = 目标,我将其添加到结果中并删除最后一个元素以尝试查找更多组合。
但显然我在这一行中遇到了错误:
temp.remove(temp.size()-1); //saying index out of bounds i am trying to remove when arraylist is empty
所以它并没有像我想的那样运行,因为如果列表为空,当前应该是 0,它甚至应该进入 if 循环并且永远不应该被删除,但它是,我不知道为什么。
谢谢。
【问题讨论】:
标签: java arrays recursion backtracking