My Java development and learning journey ------ & gt; use a recursive loop algorithm to list all the data counts in the array, ------ Recursion

Source: Internet
Author: User

My Java development and learning journey ------> use a recursive loop algorithm to list all the data counts in the array ------ Recursion

The interview questions are as follows: list all the combinations of numbers in an array, for example, columns 1 and 2 are 1, 2, 12, and 21.

(The interview questions are from the Java programmer interview book.)

The Code is as follows:

Import java. util. arrays; import java. util. using list; import java. util. list;/*** List all the array sets in an array, for example, columns 1 and 2 are 1, 2, 12, 21 */public class ListAll {public static void main (String [] args) {String [] array = new String [] {"1", "2 ", "3"}; listAll (Arrays. asList (array ),"");} /*** use Recursive Method * @ param candidate the List set recursively traversed * @ param prefix the prefix printed */public static void listAll (List <String> candidate, String prefix) {System. out. println (prefix); for (int I = 0; I <candidate. size (); I ++) {List temp = new sort List (candidate); // converts it to the sort List for ease of inserting and moving listAll (temp, prefix + temp. remove (I ));}}}

Output result:

112123131322212132323133131232321

For ease of understanding, add System. out. println ("candidate is:" + candidate) in the loop );

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 is easy to understand recursive List temp = new sort List (candidate); // converts it to the sort List for ease of inserting and moving listAll (temp, prefix + temp. remove (I ));}}


The output result is as follows:
candidate is: [1, 2, 3]1candidate is: [2, 3]12candidate is: [3]123candidate is: [2, 3]13candidate is: [2]132candidate is: [1, 2, 3]2candidate is: [1, 3]21candidate is: [3]213candidate is: [1, 3]23candidate is: [1]231candidate is: [1, 2, 3]3candidate is: [1, 2]31candidate is: [2]312candidate is: [1, 2]32candidate is: [1]321


The algorithm actually has a few numbers and is divided into several groups. For example, if [1, 2, 3], the first layer of recursion is 1-[2, 3], 2-[1, 3], 3-[1, 2]. Next, let's pass in the list and continue recursion. Taking 1-[2, 3] as an example: it can be divided into 2-[3], 3-[2], and splice these with the first layer 1 into 12-[3], 13-[2], and then the list continues recursion, so that all the combinations starting with 1 are arranged. The same is true at the beginning of 2 and 3.


If you want to print a combination of all numbers, such as 123,132,213,231,312,321

Change the code of the listAll Method 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 is easy to understand recursive List temp = new sort List (candidate); // converts it to the sort List for ease of inserting and moving listAll (temp, prefix + temp. remove (I ));}}

The output is as follows:

123132213231312321

After the annotations are removed, the output is as follows:

candidate is: [1, 2, 3]candidate is: [2, 3]candidate is: [3]123candidate is: [2, 3]candidate is: [2]132candidate is: [1, 2, 3]candidate is: [1, 3]candidate is: [3]213candidate is: [1, 3]candidate is: [1]231candidate is: [1, 2, 3]candidate is: [1, 2]candidate is: [2]312candidate is: [1, 2]candidate is: [1]321



========================================================== ========================================================== ============================

Author: Ouyang Peng: Welcome to repost. sharing with others is the source of progress!

Reprinted Please retain the original address: http://blog.csdn.net/ouyang_peng

========================================================== ========================================================== ============================


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.