###### LeetCode 46 Permutations

Given a collection of distinct 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].

	public static List<List<Integer>> permute(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
for (int i = 0; i < nums.length; i++) {
List<List<Integer>> newRes = new ArrayList<>();
for (int j = 0; j <= i; j++)
for (List<Integer> k : result) {
List<Integer> list = new ArrayList<>(k);
}
result = newRes;
System.out.println("tmp result = "+result);
}
return result;
}

System.out.println("tmp result = "+result);
nums = [0, 1, 2, 3]的打印结果：

tmp result = [[0]]
tmp result = [[1, 0], [0, 1]]
tmp result = [[2, 1, 0], [2, 0, 1], [1, 2, 0], [0, 2, 1], [1, 0, 2], [0, 1, 2]]
tmp result = [[3, 2, 1, 0], [3, 2, 0, 1], [3, 1, 2, 0], [3, 0, 2, 1], [3, 1, 0, 2], [3, 0, 1, 2], [2, 3, 1, 0], [2, 3, 0, 1], [1, 3, 2, 0], [0, 3, 2, 1], [1, 3, 0, 2], [0, 3, 1, 2], [2, 1, 3, 0], [2, 0, 3, 1], [1, 2, 3, 0], [0, 2, 3, 1], [1, 0, 3, 2], [0, 1, 3, 2], [2, 1, 0, 3], [2, 0, 1, 3], [1, 2, 0, 3], [0, 2, 1, 3], [1, 0, 2, 3], [0, 1, 2, 3]]

It could be solved using modified DFS.  Each time insert one element that has not been inserted yet.

The idea it so start with empty set. Each time , one number is introduced, for each existing solution, insert this number to all possible positions.

	public static List<List<Integer>> permute2(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
backtrack(result, new ArrayList<Integer>(), nums);
return result;
}

private static void backtrack(List<List<Integer>> result, List<Integer> tempList, int[] nums) {
if (tempList.size() == nums.length) {
} else {
for (int i = 0; i < nums.length; i++) {
if (tempList.contains(nums[i])) continue; // element already exists, skip
backtrack(result, tempList, nums);
tempList.remove(tempList.size() - 1);
}
}
}


	public List<List<Integer>> permute3(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
perm(result, nums, 0);
return result;
}

public void perm(List<List<Integer>> result, int[] nums, int pos) {
if(pos == nums.length) {
List<Integer> list = new ArrayList<Integer>();
return;
}
for(int i=pos; i<nums.length; i++) {
swap(nums, i, pos);
perm(result, nums, pos+1);
swap(nums, i, pos);
}
}

private void swap(int[] nums, int i, int j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}


#### LeetCode(46) Permutations

2014-11-21 15:54:19

#### leetcode 46. Permutations-全排列|回溯|递归|非递归

2016-05-29 22:15:10

#### 46. Permutations Leetcode Python

2015-03-03 08:26:07

#### LeetCode（46）Permutations

2015-08-31 20:07:48

#### 【leetcode】 46. Permutations ，python实现

2016-06-15 15:43:54

#### [leetcode] 46. Permutations 解题报告

2016-01-14 16:14:55

#### LeetCode(46): Permutations (C++)

2015-12-01 10:00:16

#### LeetCode - 46. Permutations

2016-06-29 11:06:26

#### [leetcode-46]Permutations(java)

2015-08-02 17:23:41

#### leetcode 46,47. Permutations I/II 全排列问题 java

2016-04-19 23:00:36

## 不良信息举报

LeetCode 46 Permutations