题目及解法均来自于 LeetCode
问题描述
Given a collection of distinct integers, return all possible permutations.
Example:
Input: [1,2,3]
Output:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
解法1
the basic idea is, to permute n numbers, we can add the nth number into the resulting List<List<Integer>>
from the n-1 numbers, in every possible position.
For example, if the input num[] is {1,2,3}: First, add 1 into the initial List<List<Integer>>
(let's call it "answer").
Then, 2 can be added in front or after 1. So we have to copy the List in answer (it's just {1}), add 2 in position 0 of {1}, then copy the original {1} again, and add 2 in position 1. Now we have an answer of {{2,1},{1,2}}. There are 2 lists in the current answer.
Then we have to add 3. first copy {2,1} and {1,2}, add 3 in position 0; then copy {2,1} and {1,2}, and add 3 into position 1, then do the same thing for position 3. Finally we have 2*3=6 lists in answer, which is what we want.
public List<List<Integer>> permute(int[] num) {
List<List<Integer>> ans = new ArrayList<List<Integer>>();
if (num.length ==0) return ans;
List<Integer> l0 = new ArrayList<Integer>();
l0.add(num[0]); //首先加入第一个数字
ans.add(l0);
for (int i = 1; i< num.length; ++i){ //依次加入后续每个数字
List<List<Integer>> new_ans = new ArrayList<List<Integer>>(); //创建新list 存储加入新数字后的结果
for (int j = 0; j<=i; ++j){ //加入的新数字可能存储的位置
for (List<Integer> l : ans){
List<Integer> new_l = new ArrayList<Integer>(l);
new_l.add(j,num[i]);
new_ans.add(new_l);
}
}
ans = new_ans;
}
return ans;
}
解法2
DFS
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> list = new ArrayList<>();
// Arrays.sort(nums); // not necessary
backtrack(list, new ArrayList<>(), nums);
return list;
}
private void backtrack(List<List<Integer>> list, List<Integer> tempList, int [] nums){
if(tempList.size() == nums.length){
list.add(new ArrayList<>(tempList)); //注意这里的操作,是加入一个新的list,内容和 tempList 一样
} else{
for(int i = 0; i < nums.length; i++){
if(tempList.contains(nums[i])) continue; // element already exists, skip
tempList.add(nums[i]);
backtrack(list, tempList, nums);
tempList.remove(tempList.size() - 1);
}
}
}
自己写的代码 ,和上面的原理差不多,只不过判断重复用的是一个 boolean 数组,backtrack里面参数 len 也是没有必要的,显得很啰嗦。
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> resList = new ArrayList<>();
if(nums == null || nums.length == 0) return resList;
boolean[] seen = new boolean[nums.length];
backtrack(resList, new ArrayList<Integer>(), nums, seen, 0);
return resList;
}
private void backtrack(List<List<Integer>> resList, ArrayList<Integer> tmpList, int[] nums, boolean[] seen, int len){
if(len == nums.length) {
resList.add(new ArrayList(tmpList));
return;
}
for(int i = 0;i < nums.length;i++){
if(seen[i] == true) continue;
tmpList.add(nums[i]);
seen[i] = true;
backtrack(resList, tmpList, nums, seen, len + 1);
tmpList.remove(tmpList.size() - 1);
seen[i] = false;
}
}
}