"Any number of numbers in Java gets all of its permutations"

Source: Internet
Author: User

Today, in the work of a problem, enter in Java , such as three-a-line number I want to get all of its permutations, such as 123,312,132,231,213,321 of these

On the internet to find someone else's algorithm, a little finishing, share to everyone code as follows

  1. Import Java.util.Arrays; Used for array output.

  2. Import java.util.LinkedList;

  3. Import java.util.List;

  4. public class Test

  5. {

  6. static String tobearranged = "123456"; The string to be arranged.

  7. Static string[] Array = Stringtostringarray (tobearranged); Convert it to an array. {"1", "2" ...}

  8. static int length=array.length; The string length.

  9. static int k=0; count when storing arrays.

  10. Static string[] result=new string[total (length)]; The size of the array is the factorial of length.

  11. public static void Main (string[] args)

  12. {

  13. Listall (arrays.aslist (array), "");

  14. System.out.println (arrays.tostring (result)); Output.

  15. }

  16. The main calculation method.

  17. public static void Listall (List candidate, String prefix)


    • {

    • int number1=1;

    • for (int i=1;i<=number;i++)

    • {

    • Number1*=i;

    • }

    • return number1;

    • }

    • }

  • {

  • if (Prefix.length () ==length)//less than the combination of string lengths is ignored.

  • {

  • Result[k++]=prefix; stored in the array.

  • }

  • for (int i=0;i

  • {

  • List tmp = new LinkedList (candidate);

  • {

  • Listall (tmp, prefix + tmp.remove (i)); The arguments in the function are parsed from the right

  • }

  • }

  • }

  • Converts a string to an array.

  • Static string[] Stringtostringarray (String s)

  • {

  • int Length=s.length ();

  • if (length<=0)

  • {

  • return new string[0];

  • }

  • String[] Result=new string[length];

  • for (int i=0;i

  • {

  • Result[i]= "" +s.charat (i);

  • }

  • return result;

  • }

  • Calculates the number factorial, which is the capacity of the resulting array.

  • static int total (int number)


"Any number of numbers in Java gets all of its permutations"

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.