Write a main function in Java, print out all the different permutations of 1-6 of these six digits

Source: Internet
Author: User
Tags character set

1, 2, 2, 3, 4, 5 these six digits, write a main function in Java, print out all the different permutations, such as: 512234, 412345, etc. requirements: "4" can not be in third place, "3" and "5" can not be connected.

Package com.test;
Import java.util.ArrayList;
 
Import java.util.List;
 /** * 1, 2, 2, 3, 4, 5 these six digits, write a main function in Java, print out all the different permutations, such as: 512234, 412345, etc. requirements: "4" can not be in third place, "3" and "5" can not be connected. * * @author SIMONE wangxinsh55@126.com 2009-6-12 * * public class Grouptest {public static list<string> List
     = new Arraylist<string> (); /** * All sorting combinations of * constructed strings * @param str will be combined into the character * @param nstr source string set/public static void Group (S)
            Tring str, string nstr) {if (Str.length ()!= nstr.length ()) {String rest = Getrest (str, NSTR);
                for (int i = 0; i < rest.length (); i++) {String temp = str + rest.substring (i, i + 1); if (Temp.indexof ("4")!= 2 && temp.indexof ("1") = = && temp.indexof ("53") = = 1) {//filter display conditions, such as
                    The decision to remove this is to list all of the character set permutations and combinations System.out.println (temp);
                    if (!list.contains (temp)) {List.add (temp);
     }               Group (temp, NSTR); /** * Removes the character to be grouped from the source string * * @param str will be combined into the character * @param nstr source Word
        String Set * @return remaining set of strings */public static string Getrest (String str, string nstr) {String rest = "";
            if (Nstr.length () > Str.length ()) {rest = Nstr; for (int i = 0; i < str.length (); i++) {rest = Rest.replacefirst (str.substring (i, i + 1), "");/Note the
    Replacefirst, instead of ReplaceAll} return rest;
        public static void Main (string[] args) {Group ("", "122345");
    System.out.println (List.tostring ()); }
}

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.