当前位置:网站首页>全排列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
边栏推荐
- Q1季度逆势增长的华为笔电,正引领PC进入“智慧办公”时代
- Is it reliable to open an account on the flush with a mobile phone? Is there any hidden danger in this way
- Ecological escort cloud service providers wave "Intel flag"
- Bi-sql like
- [untitled]
- The optimism about technology is making Dell achieve more than expected
- 4年工作经验,多线程间的5种通信方式都说不出来,你敢信?
- VB 学习笔记
- Basic knowledge of assembly language (2) -debug
- 归并排序求逆序数
猜你喜欢

Programmer: did you spend all your savings to buy a house in Shenzhen? Or return to Changsha to live a "surplus" life?

pbcms添加循环数字标签

Deploy a production cluster using Loki microservice pattern

数组中关于sizeof()和strlen

汇编语言(4)函数传参

百度语音合成语音文件并在网站中展示

Assembly language (3) 16 bit assembly basic framework and addition and subtraction loop

Basic knowledge of assembly language (2) -debug

程序员:是花光积蓄在深圳买房?还是回到长沙过“富余”生活?
The latest QQ wechat domain name anti red PHP program source code + forced jump to open
随机推荐
4 years of working experience, and you can't tell the five communication modes between multithreads. Can you believe it?
Audio PCM data calculates sound decibel value to realize simple VAD function
MySQL common basic statements (collation)
Bi-sql Union
Expectation and variance
What to learn in VB [easy to understand]
Introduction to bi-sql wildcards
脱氧核糖核酸酶I中英文说明书
Assembly language (3) 16 bit assembly basic framework and addition and subtraction loop
The optimism about technology is making Dell achieve more than expected
Use redis' sorted set to make weekly hot Reviews
Powerbi - for you who are learning
云开发技术峰会·公益编程挑战赛【火热报名中】!
天书夜读笔记——深入虚函数virtual
VB 学习笔记
利用 Redis 的 sorted set 做每周热评的功能
CCNP的BGP部分笔记
Redis persistence
Mysql database Chapter 1 Summary
Zuckerberg demonstrated four VR head display prototypes, and meta revealed the "family" of metauniverse