Full sorting. Commonly used sorting generation algorithms include the sequential number method, the Lexicographic Order Method, the Johnson (Johnson-trotter) method, the rotation method, and the shift cursor * (Gao & Wang) method.
[Question]
Given a collection of numbers, return all possible permutations.
For example,
[1,2,3]
Have the following permutations:
[1,2,3]
,[1,3,2]
,[2,1,3]
,[2,3,1]
,[3,1,2]
, And[3,2,1]
.
[Violent recursion]
This is an intuitive idea. However, note that at the beginning, the list is shared, so the previous answer will be changed, leading to errors.
Public class solution {list <integer> ret = new arraylist <list <integer> (); public list <integer> permute (INT [] num) {int Len = num. length; If (LEN = 0) return ret; List <integer> List = new arraylist <integer> (); run (list, num); return ret ;} public void run (list <integer> list, int [] num) {If (list. size () = num. length) {// note that a new list is required here, otherwise the list <integer> res = new arraylist <integer> (); res will be modified later. addall (list); ret. add (RES); return;} For (INT I = 0; I <num. length; I ++) {If (list. contains (Num [I]) {continue;} List. add (Num [I]); run (list, num); list. remove (list. indexof (Num [I]); // do not forget this step }}}
[Lexicographic method]
The C ++ STL library contains the nextpermutation () method, and its implementation is the lexicographic method.
This article briefly introduces the lexicographic Method
Summarized:
For example, the full 1234 sorting is as follows:
[Code implementation]
Because the Java list parameter is the address, remember to add a new list to the result set every time you add it, otherwise, the original list added to the result set will be changed by subsequent operations.
Public class solution {public list <integer> permute (INT [] num) {list <integer> ret = new arraylist <list <integer> (); int Len = num. length; If (LEN = 0) return ret; arrays. sort (Num); // The lexicographically ordered array must be first sorted in ascending order. // The array must be converted to the list <integer> list0 = new arraylist <integer> (); for (INT I = 0; I <Len; I ++) {list0.add (Num [I]);} // Add the list corresponding to the original array to the result, list0 cannot be added directly, because it will always change list <integer> LL = new arraylist <Integer> (); LL. addall (list0); ret. add (LL); // locate the next permutation one by one (INT I = 1; I <factorial (LEN); I ++) {ret. add (nextpermutation (list0);} return ret;}/*** lexicographically generates the next permutation ***/public list <integer> nextpermutation (list <integer> num) {// locate the last positive int I = num. size ()-1; while (I> 0 & num. get (I-1)> = num. get (I) {I --;} // find the last number larger than num [I-1] Int J = I; while (j <num. size () & num. get (j)> num. get (I-1) {J ++ ;} // Exchange num [I-1] And num [J-1] int TMP = num. get (I-1); num. set (I-1, num. get (J-1); num. set (J-1, TMP); // reverse the number reverse (Num, I, num. size ()-1); List <integer> ret = new arraylist <integer> (); ret. addall (Num); return ret;} public void reverse (list <integer> list, int begin, int end) {for (INT I = begin, j = end; I <j; I ++) {list. add (I, list. remove (j) ;}} public int factorial (int n) {return (n = 1 | N = 0 )? 1: factorial (n-1) * n ;}}
In the preceding implementation, the original array needs to be sorted in ascending order, and the next improvement to nextpermutation (list <integer> num) does not need to be sorted by num.
/*** Lexicographically generates the next permutation ***/public list <integer> nextpermutation (list <integer> num) {// locate the last positive order int I = num. size ()-1; while (I> 0 & num. get (I-1)> = num. get (I) {I --;} // if (I = 0) {reverse (Num, 0, num. size ()-1); List <integer> ret = new arraylist <integer> (); ret. addall (Num); return ret;} // find the last number larger than num [I-1] Int J = I; while (j <num. size () & num. get (j)> num. get (I-1) {J ++;} // exchange num [I-1] And num [J-1] int TMP = num. get (I-1); num. set (I-1, num. get (J-1); num. set (J-1, TMP); // reverse the number reverse (Num, I, num. size ()-1); List <integer> ret = new arraylist <integer> (); ret. addall (Num); return ret ;}
Welcome to continue optimizing the above Code!
[Leetcode] permutations solution report