Java 查找一组数字的特定大小的所有可能组合

Java 查找一组数字的特定大小的所有可能组合,java,performance,subset,combinatorics,lexicographic,Java,Performance,Subset,Combinatorics,Lexicographic,我希望解决以下问题: 给定两个整数n和k,返回13中k个数的所有可能组合。。。n 确保组合已排序 详细说明, 在每个条目中,都应该对元素进行排序。[1,4]是有效条目,而[4,1]不是 条目应在其内部进行排序 例如: 如果n=4且k=2,则解决方案为: [ [1,2],, [1,3],, [1,4],, [2,3],, [2,4],, [3,4],, ] 这就是我提出的解决方案: public ArrayList<ArrayList<Integer>> combine(i

我希望解决以下问题:

给定两个整数n和k,返回13中k个数的所有可能组合。。。n

确保组合已排序

详细说明,

  • 在每个条目中,都应该对元素进行排序。[1,4]是有效条目,而[4,1]不是
  • 条目应在其内部进行排序
  • 例如: 如果n=4且k=2,则解决方案为:

    [
    [1,2],,
    [1,3],,
    [1,4],,
    [2,3],,
    [2,4],,
    [3,4],,
    ]

    这就是我提出的解决方案:

    public ArrayList<ArrayList<Integer>> combine(int n, int k) {
    
        //variable where the resultant sets of of numbers are to be stored
        ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
    
        //finding all the subsets from 1-n of size k and storing them in result
        subset(n,result,k);
    
        //sorting the resultant set of subsets lexicographically
        Collections.sort(result,new Comparator<ArrayList<Integer>>(){
    
            @Override
            public int compare(ArrayList<Integer> a,ArrayList<Integer> b){
    
                int aSize = a.size();
                int bSize = b.size();
    
                for(int i=0;i<(int)Math.min(aSize,bSize);i++){
    
                    int comparison = Integer.compare(a.get(i),b.get(i));
                    if(comparison!=0) return comparison;
                }
                   return Integer.compare(aSize,bSize);
            }
        });
    
        return result;
    }
    
    
    void subset(int n,ArrayList<ArrayList<Integer>> result,int size){
        for(int i=0;i<(1<<n);++i){
    
            //the arraylist to be added to the result
            ArrayList<Integer> element = new ArrayList<Integer>();
    
            //iterating 2^n times since those are the total number of possible subsets
            for(int j=0;j<n;++j)
                if((i&(1<<j))>0)
                    element.add(j+1);
    
    
            //only adding the resultant subset to the result if it matches the size criteria
            if(element.size() == size)
                result.add(element);
        }
    }
    
    公共数组列表组合(int n,int k){
    //用于存储数字的结果集的变量
    ArrayList结果=新建ArrayList();
    //从大小为k的1-n中查找所有子集并将其存储在结果中
    子集(n,结果,k);
    //按字典顺序对子集的结果集进行排序
    Collections.sort(result,newcomparator(){
    @凌驾
    公共整数比较(ArrayList a、ArrayList b){
    int aSize=a.size();
    int bSize=b.size();
    
    对于(int i=0;i您可以按正确的顺序直接生成它们(伪代码):

    输出:

    [1, 2]
    [1, 3]
    [1, 4]
    [2, 3]
    [2, 4]
    [3, 4]
    

    您可以按正确的顺序直接生成它们(伪代码):

    输出:

    [1, 2]
    [1, 3]
    [1, 4]
    [2, 3]
    [2, 4]
    [3, 4]
    

    据我所知,每次递归调用基本上都在设置最左边的元素。一旦达到大小限制,就将该集添加到某个保持数据结构中,并返回递归树。是这样吗?@AdityaSatyavada我不确定我是否正确理解你。如果你遇到麻烦,我添加了实现应该有帮助。据我所知,你基本上是在每次递归调用中设置最左边的元素。一旦达到大小限制,你就将集合添加到某个保持数据结构中,并返回递归树。是这样吗?@AdityaSatyavada我不确定我是否理解正确。如果你加入,我添加了实现麻烦一下,应该会有帮助。
    int n = 4;
    int k = 2;
    Iterate(new int[k], n, k, 1, 0);
    
    [1, 2]
    [1, 3]
    [1, 4]
    [2, 3]
    [2, 4]
    [3, 4]