Title: Ask for a full array of strings.
Idea: We can generate a solution tree: Take the string "BCA", at this point, if we deal with the cur position, then we can and from the current position to start and then the position of the interchange character, so as to ensure that the two-tree each layer (the current position) of the results can appear, and then recursively go down.
Package Org;import Java.util.arrays;public class SortMethod {public static void Create (char a[], int cur, int n) {
if (cur = = N) { System.out.println (arrays.tostring (a)); return; } for (int i = cur; i < n; i++) { char tmp = a[cur]; A[cur] = A[i]; A[i] = tmp; Create (A, cur+1, n); TMP = A[cur]; A[cur] = A[i]; A[i] = tmp; } } public static void Main (string[] args) { String a = "bba"; Create (A.tochararray (), 0, A.length ());} }
Title: Generates a full array without duplicates.
Ideas: And there are some differences, first of all we have to understand that if we can ensure that there is no repetition of the possible, we can put each repetition of the characters in order to mark a subscript, then I just want to ensure that each generated by the entire sequence of repeated characters can maintain the original arrangement, that is, no reverse order, So that we can guarantee that there is no repetition of the sequence generation. Add a for-judge.
Package Org;import Java.util.arrays;public class SortMethod {public static void Create (char a[], int cur, int n) { if (cur = = N) {System.out.println (arrays.tostring (a)); Return } for (int i = cur; i < n; i++) {char tmp = a[cur]; A[cur] = A[i]; A[i] = tmp; Create (A, cur+1, N); TMP = A[cur]; A[cur] = A[i]; A[i] = tmp; }} public static void Dul (char a[], int cur, int n) {if (cur = = N) {System.out.println (arrays.t Ostring (a)); Return } for (int i = cur; i < n; i++) {int vis[] = new INT[30]; for (int j = 0; J <; J + +) Vis[j] = 0; for (int j = i-1; J >= cur; j--) vis[a[j]-' a '] = 1; if (vis[a[i]-' a '] = = 1) continue; char tmp = a[cur]; A[cur] = A[i]; A[i] = tmp; Dul (A, cur+1, N); TMP = A[cur]; A[cur] = A[i]; A[i] = tmp; }} public static void Main (string[] args) {String a = "BBA"; Create (A.tochararray (), 0, A.length ()); Dul (A.tochararray (), 0, A.length ()); }}
Full ordering of strings, non-repeating full ordering