【问题标题】:Find all possible combinations - java [closed]查找所有可能的组合 - java [关闭]
【发布时间】:2013-04-05 12:06:31
【问题描述】:

我需要找到以下方式给出的数据的组合,

jobs  #ofIntervals
----  -------------
4     1
1     2
3     2
0     3
2     3

必须根据#ofIntervals 对给定作业进行组合设置。 输出可能的组合将影响职位的位置。如果有多个具有相同#ofIntervals 的工作,那么只有更改这些工作位置才会设置新工作。给定输入的可能结果应该是这样的,

combination-1: 4 1 3 0 2    // same as input

combination-2: 4 3 1 0 2    // here as job 3 and 1 have 2 #ofIntervals they make a new combination 

combination-3: 4 1 3 2 0    // here as job 2 and 0 have 3 #ofIntervals they make a new combination

combination-4: 4 3 1 2 0

谁能帮我写一个代码或建议一个算法。

【问题讨论】:

  • 你尝试了什么?
  • 我尝试过使用树,但无法完成代码

标签: java arrays algorithm combinations


【解决方案1】:
  1. 将您的作业分成单独的集合,其中集合的每个成员都具有相同的“#of interval”值。
  2. 对于每个集合,生成一个包含该集合的所有 permutations 的集合。
  3. 生成一个集合,其中包含来自第 2 步的集合的 cartesian product

这个最终集合是您的解决方案。

【讨论】:

    【解决方案2】:

    我喜欢 mbeckish 写的答案,但这是我为实际完成工作而编写的代码:

    import java.util.ArrayList;
    import java.util.List;
    
    public class Test
    {
        public static void main(String[] args)
        {
            List<JobsInterval> jobsIntervalList = new ArrayList<JobsInterval>();
    
            jobsIntervalList.add(new JobsInterval(4, 1));
            jobsIntervalList.add(new JobsInterval(1, 2));
            jobsIntervalList.add(new JobsInterval(3, 2));
            jobsIntervalList.add(new JobsInterval(0, 3));
            jobsIntervalList.add(new JobsInterval(2, 3));
    
            printPossibleCombinations(jobsIntervalList);
        }
    
        public static void printPossibleCombinations(List<JobsInterval> list)
        {
            //Assumes the list is already in interval order.
            int currentInterval = -1;
            List<List<JobsInterval>> outerList = new ArrayList<List<JobsInterval>>(list.size());
            List<JobsInterval> innerList = null;
    
            //Loop through the list and group them into separate lists by interval.
            for (JobsInterval ji : list)
            {
                if (ji.interval != currentInterval)
                {
                    if (null != innerList)
                        outerList.add(innerList);
    
                    currentInterval = ji.interval;
                    innerList = new ArrayList<JobsInterval>(list.size());
                }
    
                innerList.add(ji);
            }
    
            if (null != innerList)
                outerList.add(innerList);
    
            print(0, outerList, null);
        }
    
        public static void permute(StringBuilder value, List<JobsInterval> list, List<String> permutations)
        {
            //Check to see if this is the last recursive call
            if (0 == list.size())
            {
                permutations.add(value.toString());
            }
            else
            {
                List<JobsInterval> subList;
    
                for (int i = 0; i < list.size(); i++)
                {
                    subList = new ArrayList<>(list);
                    subList.remove(i);
                    permute(new StringBuilder(null == value ? "" : value).append(list.get(i).jobs), subList, permutations);
                }
            }
        }
    
        public static void print(int index, List<List<JobsInterval>> list, StringBuilder value)
        {
            //Check to see if this is the last recursive call
            if (list.size() == index)
                System.out.println(value.toString());
            else
            {
                List<JobsInterval> intervalGroup = list.get(index);
                List<String> permutations = new ArrayList<String>();
    
                permute(null, intervalGroup, permutations);
    
                for (String permutation : permutations)
                    print(index+1, list, new StringBuilder(null == value ? "" : value).append(permutation));
            }
        }
    
        private static class JobsInterval
        {
            public int jobs;
            public int interval;
    
            public JobsInterval(int j, int i)
            {
                jobs = j;
                interval = i;
            }
    
            public String toString()
            {
                return new StringBuilder().append('{').append(jobs).append(", ").append(interval).append('}').toString();
            }
        }
    }
    

    【讨论】:

    • 效果很好,谢谢
    • 很好的代码,但 -1 只给出代码而不是提示/不完整的答案
    • @Doorknob 我没有写更多的原因是因为我同意mbeckish的回答。我本可以重申 mbeckish 所写的内容,但我觉得这将复制一个已经存在的答案。这就是为什么我写了我对那个答案的引用并投了赞成票。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-20
    • 1970-01-01
    • 1970-01-01
    • 2013-05-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多