Java array arrangement and combination problem summary, java array arrangement and combination

Source: Internet
Author: User

Java array arrangement and combination problem summary, java array arrangement and combination

During the interview or written examination, I have encountered the following 4 hand-Tearing algorithms for arrangement and combination. Here I will take a note and refer to them later:

1. the array without repeated elements needs to be fully arranged;
2. array with repeated elements for full arrangement;
3. For arrays without repeated elements, find the combination [subset ];
4. An array with repeated elements. Evaluate the combination;

The above four types of questions can be implemented using a unified template, as shown below:

/** [Combination & arrangement] * lists all the combinations of numbers in an array. For example, columns 1 and 2 are 1, 2, 12, and 21. * This question can be extended to four: * 1. array with no repeated numbers. Find the combination * 2. returns an array of repeated numbers. array with no repeated numbers for full arrangement * 4. array with repeated numbers for full arrangement * [general idea (recursion)]: * define a function: select a combination prefix from the candidate set candicate * remove a number from the candidate set candicate each time, and add the prefix to print the prefix; * recursively remove the next number from the new candicate, add the prefix * [for repeated control] * to use hashset to save the prefix. before printing, determine whether the hashset contains the currently generated prefix. * If not, print the prefix and add it to the hashset; if yes, do not print * [combination -- "arrangement] * just add a judgment before printing: if the candidate set candicate is empty, it indicates that the traversal is complete once and And can be printed */package xh. offer. practice; import java. util. arrays; import java. util. hashSet; import java. util. using list; import java. util. list; public class listAllGroup {public static void main (String [] args) {String [] array = {"1", "2 "}; string [] repeate = {"1", "2", "1"}; listAllGroup test = new listAllGroup (); System. out. println ("********** no repeate list ********"); test. listAllNoRepeate (Arrays. asList (Rray), ""); // initialize prefix = "" System. out. println ("*********** repeate list *******"); HashSet <String> noRepeateSet = new HashSet <String> (); test. listAllRepeate (Arrays. asList (repeate), "", noRepeateSet); System. out. println ("*************** no repeate premutation ******************** "); test. premutationNoRepeate (Arrays. asList (array), ""); System. out. println ("********************** repeate premutation *********** * ********** "); HashSet <String> repeateSet = new HashSet <String> (); test. premutationRepeate (Arrays. asList (repeate), "", repeateSet);} // public void listAllNoRepeate (List <String> candicate, String prefix) {if (prefix. length ()! = 0) System. out. println (prefix); // if the result length is not 0, the combination for (int I = 0; I <candicate. size (); I ++) {// converts a list to a linklist linked List, facilitating the operation of list <String> tempList = new shortlist <String> (candicate ); // reduce a number in templist and save the number 'string' to templist. remove (I); // recursive listAllNoRepeate (tempList, prefix + tempString) ;}// a duplicate combination is added to hashset public void listAllRepeate (List <String> candicate, string prefix, HashSet <String> res) {if (prefix. length ()! = 0 &&! Res. contains (prefix) {System. out. println (prefix); res. add (prefix) ;}for (int I = 0; I <candicate. size (); I ++) {List <String> tempList = new shortlist <String> (candicate); String tempString = tempList. remove (I); listAllRepeate (tempList, prefix + tempString, res); // recursive} // no duplicates in the full arrangement, add the judgment candicate. size () = 0 public void premutationNoRepeate (List <String> candicate, String prefix) {if (candicate. size () = 0) {System. out. println (prefix);} for (int I = 0; I <candicate. size (); I ++) {List <String> tempList = new shortlist <String> (candicate); String tempString = tempList. remove (I); premutationNoRepeate (tempList, prefix + tempString) ;}// you can add a hashset to help determine the output public void premutationRepeate (List <String> candicate, string prefix, HashSet <String> res) {if (candicate. size () = 0 &&! Res. contains (prefix) {System. out. println (prefix); res. add (prefix) ;}for (int I = 0; I <candicate. size (); I ++) {List <String> tempList = new shortlist <String> (candicate); String tempString = tempList. remove (I); premutationRepeate (tempList, prefix + tempString, res );}}}

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.