Given A collection of distinct numbers, return all possible permutations.
For example,
[1,2,3]The following permutations:
[[1,3,2], [2,1,3], [ 2,3,1], [3,1,2], [3,2,1]]
Given an array, all permutation combinations are obtained.
To make this problem is very simple, mainly to relatively fast speed, the first time to do, time-consuming 5ms, relatively slow, mainly with recursion, each to ArrayList into a number (not repeating), put enough number of result.add ();
public class Solution {public list<list<integer>> permute (int[] nums) {list<list<integer> > result = new arraylist<list<integer>> (); if (nums.length = = 0) return Result;getresult (result,nums,new Arraylist<integer> (), 0); return result; } public static void GetResult (list<list<integer>> result,int[] nums,arraylist<integer> ans,int num) { if (num = = nums.length) {result.add (new arraylist<integer> (ans)); return;} for (int i = 0; I <nums.length;i++) {if (!ans.contains (Nums[i])) {Ans.add (nums[i]); GetResult (result,nums,ans,num+1); Ans.remove ((Integer) nums[i]);}}}
There is also a way to iterate, a number when the number itself, two numbers, is the second number on the left and right of the first number of two, and so on, and so on, in the nth number, is on the basis of n-1, each vacancy (the left and right of each number, n) into the nth number of digits. Time Consuming 4ms
public class Solution {public static list<list<integer>> permute (int[] nums) {List<list<integer >> result = new arraylist<list<integer>> (); if (nums.length = = 0) return result; list<integer> l0 = new arraylist<integer> () L0.add (Nums[0]); Result.add (l0); for (int i = 1;i<nums.length ; i++) {list<list<integer>> L1 = new arraylist<list<integer>> (); for (list<integer> LL: Result) {for (int J =0; j<=i;j++) { list<integer> L2 = new arraylist<integer> (ll); L2.add (J, Nums[i]); L1.add (L2);}} result = L1;} return result;}}
Say two ways to synthesize, recursive + iterative, this is 3ms
public class Solution {public list<list<integer>> permute (int[] nums) { List<list<integer >> result = new arraylist<list<integer>> (); list<integer> element = new arraylist<integer> (); GetResult (result, element, nums, 0); return result;} private void GetResult (list<list<integer>> result, list<integer> element, int[] nums, int n) { if ( N==nums.length) { result.add (element); return; } int size = Element.size (); for (int i = 0; I <= size; i++) { list<integer> temp = new arraylist<integer> (element); Temp.add (I,nums[n]); GetResult (Result, temp, nums, n+1); } return;}}
Then there is a method, there are n digits, then there are n cases of the 1th bit, in the case of determining the first bit, the 2nd bit has the n-1 situation, and so on, using the array can make the runtime faster, time 2ms
public class Solution {public list<list<integer>> permute (int[] nums) {list<list<integer> > result = new arraylist<list<integer>> (); if (nums.length = = 0) return null;int len = Nums.length-1;result = GetResult (nums, 0, Len, result); return result;} Public list<list<integer>> GetResult (int[] nums, int k, int len,list<list<integer>> result) {if (k = = len) {list<integer> sublist = new arraylist<integer> (); for (int i = 0; i < nums.length; i++) {Sublist.add (nums[i ]);} Result.add (sublist);} else {for (int i = k; I <= len; i++) {int temp = Nums[k];nums[k] = nums[i];nums[i] = Temp;getresult (nums, K + 1, Len, R esult); int temp1 = Nums[k];nums[k] = nums[i];nums[i] = Temp1;}} return result;}}
Leetcode permutations-----java