The interview is as follows: List all the numbers in an array, for example, 1 and 2 are listed as 1,2,12,21.
(Face question from "Java Programmer interview Treasure")
The code is as follows:
Import java.util.arrays;import java.util.linkedlist;import java.util.list;/** * List all the arrays in an array, such as 1 and 2 columns for 1,2,12,21 * /public class Listall {public static void main (string[] args) {string[] array = new string[] {"1", "2", "3"};listall (ARR Ays.aslist (Array), "");} /** * Use recursive method * @param candidate recursive traversal of the List collection * @param prefix printed prefixes */public static void Listall (list<string> candidat E, String prefix) {System.out.println (prefix); for (int i = 0; i < candidate.size (); i++) {List temp = new LinkedList (ca ndidate);//Convert to LinkedList for easy placement and movement of listall (temp, prefix + temp.remove (i)); }}}
The output is:
112123131322212132323133131232321
in order to facilitate understanding, add a sentence in the Loop System.out.println ("Candidate is:" + candidate);
public static void Listall (list<string> candidate, String prefix) {System.out.println (prefix); for (int i = 0; i < Candidate.size (); i++) {System.out.println ("candidate is:" + candidate); This line of code facilitates understanding of recursive list temp = new LinkedList (candidate);//conversion to LinkedList facilitates placement and movement of listall (temp, prefix + temp.remove (i)); }}
This results in the following output:
Candidate is: [1, 2, 3]1candidate are: [2, 3]12candidate is: [3]123candidate are: [2, 3]13candidate is: [2]132candidate are: [1, 2, 3]2candidate is: [1, 3]21candidate are: [3]213candidate is: [1, 3]23candidate are: [1]231candidate is: [1, 2, 3]3cand Idate is: [1, 2]31candidate is: [2]312candidate are: [1, 2]32candidate is: [1]321
The algorithm is now a few numbers, first divided into several groups, such as [three-way] then recursive first layer is 1-[2,3],2-[1,3],3-[1,2]. Then put the list to continue recursion to 1-[2,3] as an example: also divided into 2-[3], 3-[2], and the first layer of the 1 splicing into 12-[3], 13-[2], and then list continue to recursion, so that the 1 beginning of the combination are arranged. 2, 3 begins with the same.
If you want to print out a combination of all numbers, such as 123,132,213,231,312,321
Change the Listall method code to the following
public static void Listall (list<string> candidate, String prefix) {if (Candidate.isempty ()) {System.out.println ( prefix);} for (int i = 0; i < candidate.size (), i++) {//system.out.println ("candidate is:" + candidate); This line of code facilitates understanding of recursive list temp = new LinkedList (candidate);//conversion to LinkedList facilitates placement and movement of listall (temp, prefix + temp.remove (i)); }}
Printing results are:
123132213231312321
After removing the comment, it is easier to understand and the result is:
Candidate is: [1, 2, 3]candidate are: [2, 3]candidate is: [3]123candidate are: [2, 3]candidate is: [2]132candidate] is: [1, 2 , 3]candidate is: [1, 3]candidate are: [3]213candidate is: [1, 3]candidate is: [1]231candidate are: [1, 2, 3]candidate is: [ 1, 2]candidate is: [2]312candidate are: [1, 2]candidate is: [1]321
==================================================================================================
Ouyangpeng welcome reprint, sharing with people is the source of progress!
Reprint please keep the original address : Http://blog.csdn.net/ouyang_peng
==================================================================================================
My Java Development learning journey------> Using a recursive recursion algorithm to list all combinations of data in an array