当前位置:网站首页>全排列II[存在相同元素去重 + 标准回溯]
全排列II[存在相同元素去重 + 标准回溯]
2022-06-24 21:17:00 【REN_林森】
前言
对于全排列问题,一般采用DFS进行枚举(标准回溯),而当数组中出现重复元素时,就会枚举出重复的集合。
如何去重,把相等的元素靠在一起(可排序),然后通过pre和cur是否相同,从而来去重。
一、全排列II

二、把元素靠在一起 + 标准回溯
package everyday.medium;
import java.util.*;
// 全排列II
public class PermuteUnique {
public List<List<Integer>> permuteUnique(int[] nums) {
// 先排序,让连续的数字放在相邻处,才好配合pre来去重。
Arrays.sort(nums);
List<List<Integer>> ans = new ArrayList<>();
List<Integer> el = new ArrayList<>();
// Set 记录哪些数字已经被用了。
Set<Integer> set = new HashSet<>();
// dfs搜索方式取全排列。
dfs(nums, el, ans, set);
// 返回dfs后填充好的list
return ans;
}
private void dfs(int[] nums, List<Integer> el, List<List<Integer>> ans, Set<Integer> set) {
// 当每个元素都被选择,拿去排列了,则添加该排列到list中。
if (el.size() == nums.length) {
ans.add(new ArrayList<>(el));
return;
}
// 标准回溯
// 去重,当在该位置上选择不同的两个数,而这两个数相等时,产生重复。
int pre = 1 << 31;
for (int i = 0; i < nums.length; i++) {
// 当前后选择的两个元素一样 | 这个元素已经被选了,则过。
if (pre == nums[i] || set.contains(i)) continue;
// 标准回溯
el.add(nums[i]);
set.add(i);
dfs(nums, el, ans, set);
set.remove(i);
el.remove(el.size() - 1);
// 更新pre为当前选择的元素,为下面的选择做准备。
pre = nums[i];
}
}
}
总结
1)标准回溯
2)通过把元素靠在一起,再配合pre 和 cur 变量是否相等来达到去重的目的。
参考文献
[1] LeetCode 全排列II
边栏推荐
- 欢迎来到联想智能大屏的新世界
- Basic knowledge of assembly language (2) -debug
- Zuckerberg demonstrated four VR head display prototypes, and meta revealed the "family" of metauniverse
- Picture rotation move zoom gradient
- vb学习什么[通俗易懂]
- Assembly language (4) function transfer parameters
- Welcome to the new world of Lenovo smart screen
- Introduction to bi-sql wildcards
- Why does Dell always refuse to push the ultra-thin commercial notebook to the extreme?
- 高考之后,必然会出现以下四种情况:
猜你喜欢

pbcms添加循环数字标签

粉丝福利,JVM 手册(包含 PDF)

Ideas and examples of divide and conquer

Heavyweight: the domestic ide was released, developed by Alibaba, and is completely open source! (high performance + high customization)

修身励学篇

15. several methods of thread synchronization

程序员:是花光积蓄在深圳买房?还是回到长沙过“富余”生活?

天书夜读笔记——深入虚函数virtual

LLVM TargetPassConfig

海河实验室创新联合体成立 GBASE成为首批创新联合体(信创)成员单位
随机推荐
新一代可级联的以太网远程I/O数据采集模块
Tencent has completed the comprehensive cloud launch to build the largest cloud native practice in China
Heavyweight: the domestic ide was released, developed by Alibaba, and is completely open source! (high performance + high customization)
vb学习什么[通俗易懂]
ImageView shows network pictures
JVM directive
云开发技术峰会·公益编程挑战赛【火热报名中】!
matlab 取整
4 years of working experience, and you can't tell the five communication modes between multithreads. Can you believe it?
用手机在同花顺上开户靠谱吗?这样炒股有没有什么安全隐患
excel 汉字转拼音「建议收藏」
Assembly language (3) 16 bit assembly basic framework and addition and subtraction loop
Picture rotation move zoom gradient
2种常见的设备稼动率OEE监测方法
Ideas and examples of divide and conquer
归并排序模板 & 理解
卷积与转置卷积
Super detailed description and derivation of convolution and deconvolution (deconvolution is also called transpose convolution and fractional step convolution)
[untitled]
The latest QQ wechat domain name anti red PHP program source code + forced jump to open