查找给定数字组的n个数字的所有组合

时间:2014-04-17 10:45:18

标签: java numbers set combinations

我想编写一个应用程序来查找给定数字集的n个数字的所有可能组合,并返回它们的哈希集。 例如,如果给定集合{1,5,7,9}和集合2的大小,那么我将采取:

  

[1,5] [1,7] [1,9] [5,7] [5,9]

我有以下post的代码,但我不能每次都针对给定的设置大小对其进行修改。

import java.util.ArrayList;
import java.util.Arrays;

class SumSet {
    static void sum_up_recursive(ArrayList<Integer> numbers,ArrayList<Integer> partial) {

       System.out.println("sum("+Arrays.toString(partial.toArray())+")=");

       for(int i=0;i<numbers.size();i++) 
       {
           ArrayList<Integer> remaining = new ArrayList<Integer>();
             int n = numbers.get(i);
             for (int j=i+1; j<numbers.size();j++) 
                 remaining.add(numbers.get(j));

             ArrayList<Integer> partial_rec = new ArrayList<Integer>(partial);
             partial_rec.add(n);
             sum_up_recursive(remaining,partial_rec);
       }
    }
    static void sum_up(ArrayList<Integer> numbers) {
        sum_up_recursive(numbers,new ArrayList<Integer>());
    }
    public static void main(String args[]) {
        Integer[] numbers = {1,5,7,9};
        sum_up(new ArrayList<Integer>(Arrays.asList(numbers)));
    }
}

4 个答案:

答案 0 :(得分:2)

谢谢大家...... 我也找到了这个解决方案

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;

public class Comb {

    static HashSet combine(Integer[] arr, int k, int startId, int[] branch, int numElem,HashSet arrSet)
    {
        if (numElem == k)
        {
            //System.out.println("k: "+k+(Arrays.toString(branch)));
            ArrayList<Integer> mySet = new ArrayList<Integer>();
            for(int i=0;i<branch.length;i++)
            {
                mySet.add(branch[i]);
            }
            arrSet.add(mySet);
            return arrSet;
        }

        for (int i = startId; i < arr.length; ++i)
        {
            branch[numElem++]=arr[i];
            combine(arr, k, ++startId, branch, numElem, arrSet);
            --numElem;
        }
        return arrSet;
    }

    public static void main(String args[]) 
    {
        int k = 3;
        Integer[] input ={1,5,7,9}; "ABCD".toCharArray(); 
        int[] branch = new int[k];//{0,0};//new char[k];
        HashSet arrSet=new HashSet();
        arrSet=combine(input, k, 0, branch, 0, arrSet);
    }
}

答案 1 :(得分:1)

我从niiraj874u的答案改变了递归方法以返回一个值。所以现在它提供了你想要的哈希集列表。

static List<Set<Integer>> sum_up_recursive(List<Integer> numbers,
        Set<Integer> partial , int sizeOfset) {

    List<Set<Integer>> result = new ArrayList<Set<Integer>>();

    if(partial.size() == sizeOfset)
        result.add(partial);

    for (int i = 0; i < numbers.size(); i++) {
        ArrayList<Integer> remaining = new ArrayList<Integer>();
        int n = numbers.get(i);
        for (int j = i + 1; j < numbers.size(); j++)
            remaining.add(numbers.get(j));

        Set<Integer> partial_rec = new HashSet<Integer>(partial);
        partial_rec.add(n);
        result.addAll(sum_up_recursive(remaining, partial_rec, sizeOfset));
    }

    return result;
}

public static void main(String args[]) {
    Integer[] numbers = { 1, 5, 7, 9 };
    int size = 2;

    List<Set<Integer>> allCombinations = sum_up_recursive(Arrays.asList(numbers), new HashSet<Integer>(), size);

    for (Set<Integer> set : allCombinations) {
        for (Integer num : set)
            System.out.print(num + " ");
        System.out.println();
    }
}

答案 2 :(得分:1)

package com.app.atb;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class Challenge {

    public static void main(String[] args) {

        Set<Integer> myset = new HashSet<Integer>();
        myset.add(1);
        myset.add(5);
        myset.add(7);
        myset.add(9);

        List<Set<Integer>> achieved = solveChallenge(myset);
        System.out.println(achieved);
    }

    public static List<Set<Integer>> solveChallenge(Set<Integer> myset) {

        int n = myset.size();
        Integer[] myInts = new Integer[n];
        Iterator<Integer> iterator = myset.iterator();
        int index = 0;
        while (iterator.hasNext()) {
            myInts[index] = iterator.next();
            ++index;
        }

        List<Set<Integer>> myList = new ArrayList<Set<Integer>>();
        Set<Integer> subSet;
        for (int i = 0; i < n; i++) {
            for (int j = i; j < n; j++) {

                if (j != i) {
                    subSet = new HashSet<Integer>();
                    subSet.add(myInts[i]);
                    subSet.add(myInts[j]);
                    myList.add(subSet);
                }

            }
        }
        return myList;
    }
}

答案 3 :(得分:0)

我们可以在SumSet.sum_up_recursive(ArrayList<Integer>, ArrayList<Integer>, int)方法中再添加一个参数sizeOfset,如下所示。请运行它告诉我它是否给出你想要的输出?

import java.util.ArrayList;
import java.util.Arrays;

public class SumSet {
    static void sum_up_recursive(ArrayList<Integer> numbers,
            ArrayList<Integer> partial , int sizeOfset) {

        if(partial.size() == sizeOfset)
        {
            System.out.println("sum(" + Arrays.toString(partial.toArray()) + ")=");
        }

        for (int i = 0; i < numbers.size(); i++) {
            ArrayList<Integer> remaining = new ArrayList<Integer>();
            int n = numbers.get(i);
            for (int j = i + 1; j < numbers.size(); j++)
                remaining.add(numbers.get(j));

            ArrayList<Integer> partial_rec = new ArrayList<Integer>(partial);
            partial_rec.add(n);
            sum_up_recursive(remaining, partial_rec, sizeOfset);
        }
    }

    static void sum_up(ArrayList<Integer> numbers) {
        sum_up_recursive(numbers, new ArrayList<Integer>(), 2);
    }

    public static void main(String args[]) {
        Integer[] numbers = { 1, 5, 7, 9 };
        sum_up(new ArrayList<Integer>(Arrays.asList(numbers)));
    }
}