Given a list of numbers with duplicate number in it. Find all unique permutations.
Example
For numbers [1,2,2]
the unique permutations are:
[
[1,2,2],
[2,1,2],
[2,2,1]
]
一个问题是哪里放验重的代码。在递归方法里,因为是针对index位置考虑每一种可能性,所以在index与其之后元素交换时,可以进行去重检验。
函数void helper(int[] nums, int index, List> res)的目的是:对于给定数组的index位置,
尝试每一种可能性。
class Solution {
/**
* @param nums: A list of integers.
* @return: A list of unique permutations.
*/
public List> permuteUnique(int[] nums) {
List> res = new ArrayList>();
helper(nums, 0, res);
return res;
}
boolean exist(int[] nums, int start, int end, int target) {
for(int i = start; i < end; i++) {
if(nums[i] == target) return true;
}
return false;
}
void helper(int[] nums, int index, List> res) {
if(index >= nums.length) {
List list = new ArrayList();
for(int i = 0; i < nums.length; i++) list.add(nums[i]);
res.add(list);
return;
}
for(int i = index; i < nums.length; i++) {
if(exist(nums, index, i, nums[i])) continue;
swap(nums, i , index);
helper(nums, index + 1, res);
swap(nums, i , index);
}
}
void swap(int[] nums, int i, int j) {
if(i == j) return;
else {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
}
}
另外也可以使用boolean数组标记已经选过的数字。注意去重的条件。
class Solution {
/**
* @param nums: A list of integers.
* @return: A list of unique permutations.
*/
public List> permuteUnique(int[] nums) {
List> ret = new ArrayList<>();
if (nums == null || nums.length == 0) {
ret.add(new ArrayList());
return ret;
}
Arrays.sort(nums);
boolean[] used = new boolean[nums.length];
permuteHelper(nums, 0, ret, used, new ArrayList());
return ret;
}
private void permuteHelper(int[] nums, int len, List> ret, boolean[] used, List curr) {
if (len == nums.length) {
ret.add(new ArrayList(curr));
return;
}
for (int i = 0; i < nums.length; i++) {
if (!used[i]) {
if (i > 0 && nums[i] == nums[i - 1] && used[i - 1]) {//重要
continue;
}
used[i] = true;
curr.add(nums[i]);
permuteHelper(nums, len + 1, ret, used, curr);
curr.remove(curr.size() - 1);
used[i] = false;
}
}
}
}
非递归方法则要使用一个额外的List。对结果list中的每一个list,给定位置i,swap所有可能的i值。新的排列会加入结果list中。
class Solution {
/**
* @param nums: A list of integers.
* @return: A list of unique permutations.
*/
public List> permuteUnique(int[] nums) {
List> res = new ArrayList>();
List> next = new ArrayList>();
List list = new ArrayList();
for(int i = 0; i < nums.length; i++) list.add(nums[i]);//先加入种子序列
res.add(list);
for(int i = 0; i < nums.length; i++) {
for(int j = 0; j < res.size(); j++) {
List tmp = res.get(j);
boolean[] visit = new boolean[nums.length];
for(int k = i + 1; k < tmp.size(); k++) {//每次从i+1位置开始置换。避免重复
if(exist(tmp, visit, tmp.get(k)) || visit[k] || tmp.get(k) == tmp.get(i)) continue;
swap(tmp, i, k);
next.add(new ArrayList(tmp));
swap(tmp, i, k);
visit[k] = true;
}
}
res.addAll(next);
next.clear();
}
return res;
}
void swap(List list, int i, int j) {
int tmp = list.get(i);
list.set(i, list.get(j));
list.set(j, tmp);
}
boolean exist(List list, boolean[] visit, int target) {
for(int i = 0; i < list.size(); i++) {
if(visit[i] && list.get(i) == target) return true;
}
return false;
}
}