Given A collection of numbers, return all possible permutations.
For example,
[1,2,3]The following permutations:
[1,2,3],,,,, [1,3,2] [2,1,3] [2,3,1] [3,1,2] and [3,2,1] .
Public classSolution {//full arrangement: Constructs a recursive function, a parameter of a function that represents the index to begin arranging, an index that represents the final arrangement//each one is exchanged with the start bit, and then recursively start+1 to end, paying attention to the saving of the resultList<integer>seq; List<List<Integer>>Res; PublicList<list<integer>> Permute (int[] nums) {SEQ=NewArraylist<integer>(); Res=NewArraylist<list<integer>>(); Findpermute (Nums,0,nums.length-1); returnRes; } Public voidFindpermute (int[]nums,intStartintend) { if(start>end) {Res.add (Newarraylist<integer> (seq));//Note to re-new a return ; } for(inti=start;i<=end;i++) {swap (nums,start,i); Seq.add (Nums[start]); Findpermute (Nums,start+1, end); Seq.remove (Seq.size ()-1);//Note Deleteswap (nums,start,i); } } Public voidSwapint[] Nums,intIintj) { inttemp=Nums[i]; Nums[i]=Nums[j]; NUMS[J]=temp; }}
[Leedcode 46] Permutations