LeetCode刷题记69
47. 全排列 II
class Solution {
public void F(List<List<Integer>> ans, int[] nums, int cur, Set<List<Integer>> set) {
if (cur == nums.length) {
List<Integer> tmp = new ArrayList<Integer>();
for (int num : nums) {
tmp.add(num);
}
if (set.contains(tmp) == false) {
set.add(tmp);
ans.add(tmp);
}
return;
}
for (int i = cur; i < nums.length; i ++) {
if (i != cur && nums[i] == nums[cur]) continue;
int tmp = nums[cur];
nums[cur] = nums[i];
nums[i] = tmp;
F(ans, nums, cur + 1, set);
nums[i] = nums[cur];
nums[cur] = tmp;
}
}
public List<List<Integer>> permuteUnique(int[] nums) {
List<List<Integer>> ans = new ArrayList();
F(ans, nums, 0, new HashSet<List<Integer>>());
return ans;
}
}
1/5
69/150