描述
給定一個數字列表,返回其所有可能的排列。
注意事項
你可以假設沒有重復數字。
樣例
給出列表 [1,2,2],不同的排列有:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
挑戰
使用遞歸和非遞歸分別完成該題。
代碼
- 遞歸
public class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> results = new ArrayList<>();
//注意空指針和空集的區別,空指針返回[],空集返回[[]]
if (nums == null) {
return results;
}
if (nums.length == 0) {
//List是更抽象的接口類,不能用于實例化,實例化用ArrayList
results.add(new ArrayList<Integer>());
return results;
}
List<Integer> list = new ArrayList<>();
helper(nums, list, results);
return results;
}
private void helper(int[] nums,
List<Integer> list,
List<List<Integer>> results) {
//遞歸的出口
// 同[求子集](http://www.lxweimin.com/p/b494dde0ffdb)有區別,
// 子集的 startIndex 每進入一輪遞歸 +1,startIndex >= nums.length 則不再進入遞歸
if (list.size() == nums.length) {
results.add(new ArrayList<Integer>(list));
return;
}
//遞歸的拆解,題目聲明無重復元素,所以要有一個去重的過程
for (int i = 0; i < nums.length; i++) {
if (list.contains(nums[i])) {
continue;
}
list.add(nums[i]);
helper(nums, list, results);
list.remove(list.size() - 1);
}
}
}
- 非遞歸
class Solution {
/**
* @param nums: A list of integers.
* @return: A list of permutations.
*/
public List<List<Integer>> permute(int[] nums) {
ArrayList<List<Integer>> permutations
= new ArrayList<List<Integer>>();
if (nums == null) {
return permutations;
}
if (nums.length == 0) {
permutations.add(new ArrayList<Integer>());
return permutations;
}
int n = nums.length;
ArrayList<Integer> stack = new ArrayList<Integer>();
stack.add(-1);
while (stack.size() != 0) {
Integer last = stack.get(stack.size() - 1);
stack.remove(stack.size() - 1);
// increase the last number
int next = -1;
for (int i = last + 1; i < n; i++) {
if (!stack.contains(i)) {
next = i;
break;
}
}
if (next == -1) {
continue;
}
// generate the next permutation
stack.add(next);
for (int i = 0; i < n; i++) {
if (!stack.contains(i)) {
stack.add(i);
}
}
// copy to permutations set
ArrayList<Integer> permutation = new ArrayList<Integer>();
for (int i = 0; i < n; i++) {
permutation.add(nums[stack.get(i)]);
}
permutations.add(permutation);
}
return permutations;
}
}
本題遞歸過程
DFS的關鍵在于理解遞歸在for循環中的變化過程,以本題[1, 2, 3]為例,第一個for循環時,先將1加入list,list[1],然后程序運行到helper(rst, list, nums)時進入第二層遞歸,for循環到1,list中有1,continue,for循環到2,把2加入list,list[1, 2],程序再次運行到helper(rst, list, nums)時進入第三層遞歸,for循環到1,2,list中已經存在1,2,continue,for循環到3,把3加入list,進入第四層遞歸,此時list.size() == num.length,將list加入rst,return語句直接退出第四層遞歸(注意到目前為止盡管運行了4層遞歸仍然沒有執行過程序的最后一句),退出第四層遞歸后開始執行第三層遞歸的最后一句,從list里刪除最后一個元素,此時第三層for循環已經執行到i=2,執行完list.remove(list.size() - 1)就相當于第三層遞歸整個結束了,退出第三層遞歸,此時list為[1,2],開始執行第二層遞歸,此時第二層遞歸的i=1,for循環還沒結束,執行完list.remove(list.size() - 1)后, list變成[1],for循環中i等于2,將nums[2]=3加入list,list為[1, 3],繼續執行helper(rst, list, nums),再次進入第五次遞歸,list中有1,continue,for循環到2(i = 1)將2加入list,進入下一層第六次遞歸,滿足出口條件,return,本層第六次遞歸結束,第五次遞歸執行到for循環中i=1的最后一句list.remove(list.size() - 1),從list里移除2使list從[1, 3 , 2]變回[1, 3]開始執行for循環i = 2,發現list存在3,continue,直接跳出for循環結束第五次遞歸重新進入第二層遞歸,第二遞歸也執行到了for循環i = 2的最后一句,執行list.remove(list.size() - 1)將list變成[1]后,退出第二層遞歸,開始執行第一層遞歸的for循環中i = 0的最后一句list.remove(list.size() - 1)將list變成空集,然后執行for循環i =1,list變成[2]之后執行遞歸list變成[2,1],遞歸[2,1,3]接下來過程和之前描述過程類似
總結
本題和子集問題基本上是一個套路,細節上就在于本題沒有定義一個startIndex來限制循環的起始位置,也就實現了排列的目的