Title: Given A collection of distinct numbers, return all possible permutations.
Analysis: There are two ways to solve the problem
method One:1) The first element is exchanged in turn with all elements;
2) After the exchange, it can be seen as two parts: the first element and its subsequent elements;
3) The following elements can also be regarded as an array to be arranged, recursive, when the remainder of the remaining part only one element, get a permutation;
4) Restore the elements exchanged in the 1th step and exchange them with the next bit element.
Repeat the 4 steps above until you swap to the last element. (refer to the sword refers to offer explanation)
Method two: Dictionary sequence, according to the current sequence, generate the next sequence, Exchange + reverse order.
Code:
Method One: Non-dictionary order
public static void main (string[] args) { //
TODO auto-generated Method stub
//
define Array /span> int [] array = {1,2,3}; List <List<Integer>> list = new arraylist<list< Integer>> (); int start = 0 // not heavy, non-dictionary order list = Getpermutations (array, list, start);
/*** Method One * Do not go heavy, non-dictionary order *@paramArray *@return */ Public StaticList<list<integer>> Getpermutations (int[] array,list<list<integer>> List,intstart) { if(start = = Array.Length) {//Traverse to the last one, get an arrangementlist<integer> item =NewArraylist<integer>(Array.Length); for(inti = 0,len = Array.Length; i < Len; ++i) Item.add (Array[i]); List.add (item); } Else { //number of exchanges required for(inti = Start,len = Array.Length; i < Len; ++i) {//The first element, in turn, is exchanged with all subsequent elements//Exchangeswap (Array,i,start); //recursion in the loopGetpermutations (Array,list,start + 1);//A string is divided into two parts, the first element and all elements after the first element//Backtrackingswap (Array,i,start); } } returnlist; }
Method Two: Dictionary order, call next permutation
/*** Method Two * not to weight, dictionary order *@return */ Public StaticList<list<integer>> permutations (int[] array,list<list<integer>>list) { //A total of n! operations are required for(inti = 0,len = factorial (array.length); i < Len; ++i) { if(i = = 0) {List<Integer> item =NewArraylist<integer>(); for(intj = 0,l = Array.Length; J < L; ++j) Item.add (Array[j]); List.add (item); } Else{List<Integer> item =NewArraylist<integer>(); Item=nextpermutation (array); List.add (item); } } returnlist; } /*** Get an arrangement of the next dictionary order *@return */ Public StaticList<integer> Nextpermutation (int[] nums) { if(Nums = =NULL) return NULL; if(Nums.length = = 0) return NewArraylist<integer>(); //an array of length 1 if(Nums.length = = 1) { return NewArraylist<integer> (nums[0]); } //Store Resultslist<integer> result =NewArraylist<integer>(); //find the first element that does not satisfy the reverse order from the back inti = Nums.length-2; for(; I >= 0 && nums[i] > nums[i + 1];--i); //find the smallest element larger than Nums[i] starting from the i+1 position if(I >= 0){ intj = i + 1; for(; J < nums.length && Nums[j] > Nums[i]; + +j); Swap (Nums,i,j-1);//Exchange, attention is exchanged with j-1 } //reverse the element after I (this contains a special case, if the permutation is already the largest in the dictionary order, then the next sequence should be the most dictionary order, therefore, directly here to reverse the position) intK = Nums.length-1; I++; for(; i < K; i++, k--) Swap (Nums, I, K); for(intL = 0,len = Nums.length; l < Len; ++l) Result.add (Nums[l]); returnresult; }
Permutations (Java, no repeating elements, dictionary order + non-dictionary order)