简体   繁体   中英

How to I generate all the possible permutations 4 in length from 5 numbers?

So I hoped to figure out the code that would finish this:

public List<List<Integer>> create4(List<Integer> dice) {
   //unknown code



        return listOf4s;
    }

So dice would always be a List of 5 Integers,

from the 5 Integers I would like to create a List every possible permutation using only 4 numbers.

I hope I have made this clear enough.

I have searched around but have not quite seen anything I could use. Any help would be great!

There is a lot of example code on the web that you can repurpose for your needs. For example, have a look at this permutation generator article to get started.

I have not tried this out, but hopefully this helps.

public List<List<Integer>> createN(List<Integer> dice) {
    if (dice.size() == 1) {
        List<List<Integer>> permutations = new ArrayList<List<Integer>>();
        permutations.add(dice);
        return permutations;
    }
    else {
       List<List<Integer>> permuations = new ArrayList<List<Integer>>();
       for (int i=0;i<dice.size();i++) {
           List<Integer> remainingElementsInPermutationSet = new ArrayList<Integer>();
           Integer firstElementInPermutationSet = null; 
           for (int j=0;j<dice.size();j++) {
               if (i==j) {
                  firstElementInPermutationSet = dice.get(j);
               }
               else {
                   remainingElementsInPermutationSet.add(dice.get(j));   
               }
           }
           List<List<Integer>> remainderPermutations = createN(remainingElementsInPermutationSet);
           for (List<Integer> permutationRemainer : remainderPermutations) {
               List<Integer> permutation = new ArrayList<Integer>();
               permutation.add(firstElementInPermutationSet);
               permutation.addAll(permutationRemainer);
               permuations.add(permutation);
           }
       }
       return permutations:
    }

} 

I tend to like shorter code ;)

public List<List<Integer>> create4(List<Integer> dice) {
    List<List<Integer>> listOf4s = new ArrayList<List<Integer>>();
    for(Integer num : dice) {
        List<Integer> dice2 = new ArrayList<Integer>(dice);
        dices2.remove(num);
        listOf4s.add(dices2);
    }
    return listOf4s;
}

This is what I did using @btreat 's idea and it worked great.

public List<List<Integer>> create4(List<Integer> dice) {
        List<List<Integer>> permutations = new ArrayList<List<Integer>>();
        for(int i = 0; i < dice.size(); i++) {
            List<Integer> includedPermutation = new ArrayList<Integer>();
            for(int j = 0; j < dice.size(); j++) {
                if(i!=j) {
                    includedPermutation.add(dice.get(j));
                }
            }
            permutations.add(includedPermutation);
        }

        return permutations;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM