"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)