当前位置: 首页 > 知识库问答 >
问题:

数组组合和-动态规划-需要修复

穆阳嘉
2023-03-14

我已经实现了代码来输出从输入数组的元素中获得目标和的所有不同的唯一可能性。例如,给定arr-

public class CombinationSum {

    public static void main(String[] args){

        List<Integer> temp = new ArrayList<Integer>();
        List<Set<Integer>> resultList = new ArrayList<Set<Integer>>();
        int[] arr={10,1,2,7,6,3,5};
        Arrays.sort(arr);
        System.out.println(Arrays.toString(arr));

        int target=8;
        sumCombinations(resultList, temp, target, arr, 0);
        System.out.printf("target is %s; resultList is %s%n",target,resultList);

        int target2=33;
        List<Integer> temp2 = new ArrayList<Integer>();
        List<Set<Integer>> resultList2 = new ArrayList<Set<Integer>>();
        sumCombinations(resultList2, temp2, target2, arr, 0);
        System.out.printf("target is %s; resultList is %s%n",target2,resultList2);          
    }

    public static void sumCombinations(List<Set<Integer>> resultList, List<Integer> temp, int target, int[] arr,
            int start){

       for (int i=start;i<arr.length;i++){
           if (arr[i]==target){
               temp.add(arr[i]);
               Set<Integer> scratch = new HashSet<Integer>(temp);
               if (!resultList.contains(scratch))
                   resultList.add(scratch);
           }
           else if (target>arr[i]){
               temp.add(arr[i]);
               sumCombinations(resultList, temp, target-arr[i], arr, start+1);
           }
           else  return; 
               if (temp.size()>0)
              temp.remove(temp.size()-1);
           }
       }
    }

输出:

target is 8; resultList is [[1, 2, 5], [1, 7], [2, 3], [2, 6], [3, 5]]`

target is 33; resultList is [[1, 2, 3, 7, 10], [1, 2, 5, 10], [1, 2, 6, 7, 10], 
[1, 2, 10], [1, 3, 6, 10], [1, 3, 5, 7, 10], [1, 3, 6, 7, 10], [1, 5, 7, 10], 
[1, 5, 6, 10], [1, 5, 6, 7], [1, 6, 7], [1, 6, 10], [2, 3, 6, 10], [2, 5, 7, 10],
[2, 6, 7, 10], [2, 3, 5, 10], [2, 3, 5, 6, 7, 10], [2, 3, 7], [2, 5, 6, 10], 
[2, 5, 7], [2, 5, 6, 7], [2, 6, 7], [2, 7, 10], [3, 7, 10], [3, 5, 7, 10], 
[3, 5, 6, 10], [3, 6, 7], [3, 5, 6, 7], [3, 5, 10], [3, 6, 7, 10], [3, 10], 
[5, 6, 7], [5, 6, 7, 10], [5, 6, 10], [5, 7], [6, 7], [6, 7, 10]]

共有1个答案

曹鸿风
2023-03-14

您的递归调用

sumCombinations(resultList, temp, target - arr[i], arr, start + 1);

应该是这样的:

sumCombinations(resultList, temp, target - arr[i], arr, i + 1);

因为在将数字i添加到拾取前一个0的所有组合中时,就会执行此递归。。已经考虑了i-1编号,您只需在最后一次添加后调用sumCombinations即可测试组合。

这导致一些数字被重新考虑并多次添加,例如,8的错误解[2,3]实际上是[2,3,3],当转换为一个集合时,删除了重复的3。

 类似资料:
  • 我正在探索动态编程设计方法如何与问题的潜在组合属性相关联。 为此,我正在研究硬币兑换问题的典型实例:让和

  • 我应该对两个分区问题的动态规划实现应用什么修改来解决以下任务: 给你一个正整数数组作为输入,表示为C。程序应该决定是否可以将数组划分为两个相等的子序列。您可以从数组中删除一些元素,但不是全部,以使此类分区可行。 例: 假设输入是4 5 11 17 9。如果我们去掉11和17,两个分区是可能的。我问题是,我应该对我的两个分区实现进行什么调整,以确定两个分区是否可能(可能需要或可能不需要删除某些元素)

  • 我试图从黑莓的本机日历中读取“day”值,该值以整数形式返回,并映射到一周中每一天的值。这些值如下所示: 周一:32768 星期二:16384 周三:8192 周四:4096 周五:2048 sat:1024 孙:65536 如果事件发生在一天内,我可以看到值是否为mon/tue/we/thu/fri/sat/sun 值也与星期一值相同。 现在的问题是,如果事件发生在两天或三天以上 返回所选天数的

  • 基本上,我正在尝试使用动态编程在python中实现combination sum II,我的目标是创建一个时间复杂度不为O(2^n)的程序,但我遇到了很多问题,在任何地方都找不到解决方案。下面是我迄今为止得到的代码,但它似乎没有给出任何输出。 预期输出:[1,2,3],[1,5],[2,4] 实际产出:字面上没有

  • 我问了这个问题 求和的方法数S与N个数求和的所有方法从给定集合中求和给定数(允许重复) 不太明白那里的答案, 我写了两种方法来解决一个问题: 用数字N(允许重复)找出求和S的方法数 sum=4,number=1,2,3答案是1111、22、1122、31、13、1212、2112、2212 在一种方法中我使用记忆,而在另一种方法中我不使用。不知怎的,在我的机器中,记忆版本的运行速度比非记忆版本慢得

  • 我有一个子集问题的工作代码,如果它发现一个子集等于所需的目标,它可以打印数字。 > 我想打印给定目标的所有可能的子集,我不知道要为此更改什么。 我如何让它对负数起作用?