My code:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Solution {
public List<List<Integer>> permuteUnique(int[] nums) {
if (nums == null || nums.length == 0)
return null;
ArrayList<List<Integer>> result = new ArrayList<List<Integer>>();
ArrayList<Integer> permutations = new ArrayList<Integer>();
boolean[] isVisited = new boolean[nums.length];
Arrays.sort(nums);
getPemutations(nums, isVisited, result, permutations);
return result;
}
private void getPemutations(int[] nums, boolean[] isVisited,
ArrayList<List<Integer>> result, ArrayList<Integer> permutations) {
if (permutations.size() == nums.length)
result.add(new ArrayList<Integer>(permutations));
else {
for (int i = 0; i < nums.length; i++) {
if (isVisited[i])
continue;
else if (i > 0 && nums[i] == nums[i - 1] && !isVisited[i - 1])
continue;
permutations.add(nums[i]);
isVisited[i] = true;
getPemutations(nums, isVisited, result, permutations);
isVisited[i] = false;
permutations.remove(permutations.size() - 1);
}
}
}
}
My test result:
和之前的 permutations 相比,多了重復。那么怎么解決這個重復問題呢?
比如, 1,1,2
一開始進入,
1,1,2
1,2,1
會出現這些情況,然后逐步退棧,直到ArrayList<Integer> permutations 空了。
然后訪問, nums[1] -> 1
但其實,這個情況和之前 nums[0] -> 1 的情況一模一樣,完全沒必要再走一遍,還會多出重復的結果。那么,怎么解決呢?
我的打算是,每一層遞歸,建立一個哈希表。不需要作為參數傳入,就保留在每一層。
然后,遍歷一個數,就存進去,并且在遍歷的時候判斷下,該數是否已經存在于哈希表了。如果已經存在了,說明之前,重點, 之前 已經遍歷過了該數。就不需要再遍歷一次了。
但是無疑開銷是巨大的,需要在每一層都申請哈希表,太浪費空間了。
于是上網查了,發現一種更簡潔的做法,我也知道一定會有這么簡潔的做法的。
提前先將nums數組排下序,然后重復的數組就會靠在一塊兒了。
比如,1,1,2
那么,當我訪問第二個1時,可能會有兩種情況。
第一種情況,目前我位于第二層遞歸,前面已經有了一個1進入了permutations,所以我應該處理這個1.
第二種情況,目前我位于第一層遞歸,目前permutations是空的。但因為我之前的1已經完完整整處理過了一次類似的過程, 所以這個1就可以直接跳過了。
那么怎么區分這兩種情況呢?
看,isVisited[i - 1] 是否為真。
如果為真,說明前面的1已經被訪問過了,那么當前就在第二層遞歸。處理。
如果為假,如果前面的1未被訪問,說明之前已經處理過一輪了,這一輪不需要再處理了。
那么,直接 continue;
然后就可以解決重復的問題了。
推薦一個博客,講的比我清楚。
http://bangbingsyb.blogspot.com/2014/11/leetcode-permutations-i-ii.html
**
總結: backtracing, 如何解決遞歸中的重復數字問題。
**
Anyway, Good luck, Richardo!
My code:
public class Solution {
public List<List<Integer>> permuteUnique(int[] nums) {
ArrayList<List<Integer>> ret = new ArrayList<List<Integer>>();
if (nums == null || nums.length == 0)
return ret;
Arrays.sort(nums);
ArrayList<Integer> group = new ArrayList<Integer>();
boolean[] isVisited = new boolean[nums.length];
helper(nums.length, nums, isVisited, group, ret);
return ret;
}
private void helper(int k, int[] nums, boolean[] isVisited,
ArrayList<Integer> group, ArrayList<List<Integer>> ret) {
if (k <= 0) {
ret.add(new ArrayList<Integer>(group));
return;
}
else {
for (int i = 0; i < nums.length; i++) {
if (isVisited[i])
continue;
else if (i > 0 && nums[i] == nums[i - 1] && isVisited[i - 1]) {
continue;
}
else {
group.add(nums[i]);
isVisited[i] = true;
helper(k - 1, nums, isVisited, group, ret);
isVisited[i] = false;
group.remove(group.size() - 1);
}
}
}
}
}
這次寫的,沒能處理的很好。在于,重復元素。
我的做法是,當前后相等時,把前個元素布爾位置為false。
然后以后dfs的時候,即使碰到他,也不會加進group,從而避免重復情況。
但是,其實可以直接把這種情況整個忽略了。
因為這和之前,前個元素作為開頭所產生的permutation完全相同,不需要再次dfs了。
所以,改了之后,速度提升了許多。
Anyway, Good luck, Richardo!