Home > Back-end >  The whole arrangement algorithm of bugs
The whole arrangement algorithm of bugs

Time:10-24

Title description
Enter a string, according to the dictionary sequence to print out all of the characters in this string, such as the input string ABC, then print out in dictionary sequence by the characters a, b, c can arrange all string ABC, acb, bac, bca, cab and cba,
My code:
 public class Permutation {
Private static ArrayList Result=new ArrayList (a);
Public static void main (String [] args) {
String STR="ABC".
The permutation (STR);
For (int I=0; I & lt; Result. The size (); I++) {
System. The out. Println (result. The get (I));
}
}

Public static ArrayList Permutation (String STR) {
Char [] chars=STR. ToCharArray ();
The Arrays. Sort (chars);
Result. The add (String. The valueOf (chars));
Swap (chars);
return result;
}
Public static void swap (char [] chars) {
If (chars. Length==1) {
return;
}

Swap (Arrays. CopyOfRange (chars 1, chars. Length));
For (int I=1; I & lt; Chars. Length; I++) {
Char [] copy=Arrays. CopyOf (chars, chars. Length);
Char temp=chars [I];
For (int j=I - 1; J & gt;=0; J -) {
Copy [j + 1)=copy [j];
}
Copy [0]=temp;
Result. The add (String. The valueOf (copy));
Swap (Arrays. CopyOfRange (1 copy, copy the length));

}
}
}

Results:

Have master answer, how change can output the missing characters, I don't know how to put every time the result of the exchange and the original string concatenation, thank you
  • Related