Sword refers to the offer series---string arrangement (not very understanding)

Source: Internet
Author: User

"title" Enter a string that prints out all the permutations of the characters in the string in the dictionary order.

For example, enter the string ABC and print out all the strings abc,acb,bac,bca,cab and CBA that can be arranged by the character A,b,c.

The results are output in alphabetical order.

1  PackageCom.exe5.offer;2 3 Importjava.util.ArrayList;4 Importjava.util.Collections;5 6 /**237 * Enter a string to print out all the permutations of the characters in the string in the dictionary order. 8 * For example, enter the string ABC, then print out all the strings abc,acb,bac,bca,cab and CBA that can be arranged by the character A,b,c. 9 * Results should be output in alphabetical order. Ten  * @authorWGS One  * A  */ -  Public classSequenceofarrays { -  the      PublicArraylist<string>permutation (String str) { -Arraylist<string> list=NewArraylist<string>(); -         if(str==NULL|| Str.length () <=0) -             returnlist; +List=permutation (List,str.tochararray (), 0, Str.length ()); - Collections.sort (list); +         returnlist; A          at     } -     Privatearraylist<string> permutation (arraylist<string> list,Char[] str,intBeginintlength) { -         if(begin==length-1){ -             if(!list.contains (string.valueof (str))) { - List.add (string.valueof (str)); -             } in}Else{ -              for(inti=begin;i<length;i++){ to                 if(I==begin | | str[i]!=Str[begin]) { + swap (str,begin,i); -Permutation (list,str,begin+1, length); the swap (str,begin,i); *                 } $             }Panax Notoginseng              -         } the         returnlist; +     } A     Private voidSwapChar[] str,intBegininti) { the         Chartemp=Str[begin]; +str[begin]=Str[i]; -str[i]=temp; $          $     } -      Public Static voidMain (string[] args) { -Sequenceofarrays s=Newsequenceofarrays (); theString str= "ABC"; -Arraylist<string> list=s.permutation (str);Wuyi          for(String s2:list) { theSystem.out.println (s2+ ""); -         } Wu  -     } About  $}

Sword refers to the offer series---string arrangement (not very understanding)

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.