Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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 组合和调试_Java_Arrays_Recursion_Backtracking - Fatal编程技术网

Java 组合和调试

Java 组合和调试,java,arrays,recursion,backtracking,Java,Arrays,Recursion,Backtracking,我已经写了一些代码试图解决这个挑战,但它不起作用,我似乎无法找出哪里出了问题,我可以在网上找到答案,但这不是我试图了解为什么我的代码不起作用的重点 问题: 给定一组候选编号(候选编号)(无重复)和目标编号(目标编号),查找候选编号与目标编号相加的所有唯一组合 同一重复次数可从候选人中选择,次数不限 Input: candidates = [2,3,6,7], target = 7, A solution set is: [ [7], [2,2,3] ] 这就是我想到的: class S

我已经写了一些代码试图解决这个挑战,但它不起作用,我似乎无法找出哪里出了问题,我可以在网上找到答案,但这不是我试图了解为什么我的代码不起作用的重点

问题:

给定一组候选编号(候选编号)(无重复)和目标编号(目标编号),查找候选编号与目标编号相加的所有唯一组合

同一重复次数可从候选人中选择,次数不限

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);
        }
    }
    
}
所以它不是按照我的想法运行的,因为如果列表是空的,那么当前应该是0,它甚至应该进入if循环,并且永远不应该被删除,但它是空的,我不知道为什么

谢谢。

试试这个

static List<List<Integer>> combinationSum(int[] candidates, int target) {
    int size = candidates.length;
    List<List<Integer>> result = new ArrayList<>();
    new Object() {
        void search(int index, int sum, List<Integer> selected) {
            if (index >= size) {
                if (sum == target)
                    result.add(new ArrayList<>(selected));
            } else {
                int candidate = candidates[index];
                List<Integer> nextSelected = new ArrayList<>(selected);
                for (int nextSum = sum; nextSum <= target; nextSum += candidate, nextSelected.add(candidate))
                    search(index + 1, nextSum, nextSelected);
            }
        }
    }.search(0, 0, new ArrayList<>());
    return result;
}

if的主要问题是在if(current>=target)if语句中尝试回滚当前变量值并再次调用helper方法。您可以使用for循环自动执行此操作,并在返回后删除添加的数字。然后使用函数return更新起始值,以便从您停止的位置继续,从而消除重复项

因为for-loop num[i]永远不会越界,所以你不必担心

if(start>=nums.length){
            return;
        }
这是使用您的解决方案方法的工作版本

public static 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;
}

public static int helper(List<List<Integer>> res, List<Integer> temp, int[] nums, int target, int current, int start){

    if(current>=target){
            
        if(current==target){
            res.add(new ArrayList<>(temp));
        }
        return start + 1;
    }
    for(int i=start; i<nums.length; i++){
        temp.add(nums[i]);
        start = helper(res,temp,nums,target,current+nums[i],start);
        temp.remove(temp.size()-1);
    }
    return start;
}
[[7], [2, 2, 3]]
if(start>=nums.length){
            return;
        }
public static 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;
}

public static int helper(List<List<Integer>> res, List<Integer> temp, int[] nums, int target, int current, int start){

    if(current>=target){
            
        if(current==target){
            res.add(new ArrayList<>(temp));
        }
        return start + 1;
    }
    for(int i=start; i<nums.length; i++){
        temp.add(nums[i]);
        start = helper(res,temp,nums,target,current+nums[i],start);
        temp.remove(temp.size()-1);
    }
    return start;
}
public static void main(String []args){
        List<List<Integer>> res = combinationSum(new int[] {2,3,6,7}, 7);
        System.out.println(res);
}
[[2, 2, 3], [7]]