当前位置:网站首页>Full arrangement ii[duplicate removal of the same elements + standard backtracking]
Full arrangement ii[duplicate removal of the same elements + standard backtracking]
2022-06-25 01:37:00 【REN_ Linsen】
Full Permutation II
Preface
For the full permutation problem , It is generally used DFS Enumerate ( Standard backtracking ), And when there are duplicate elements in the array , You'll find repeated sets .
How to go heavy , Put equal elements together ( Sortable ), And then through pre and cur Are they the same? , So as to come and go again .
One 、 Full Permutation II
Two 、 Put the elements together + Standard backtracking
package everyday.medium;
import java.util.*;
// Full Permutation II
public class PermuteUnique {
public List<List<Integer>> permuteUnique(int[] nums) {
// Prioritize , Place consecutive numbers next to each other , To cooperate pre To and fro .
Arrays.sort(nums);
List<List<Integer>> ans = new ArrayList<>();
List<Integer> el = new ArrayList<>();
// Set Record which numbers have been used .
Set<Integer> set = new HashSet<>();
// dfs The search method is full arrangement .
dfs(nums, el, ans, set);
// return dfs After filling list
return ans;
}
private void dfs(int[] nums, List<Integer> el, List<List<Integer>> ans, Set<Integer> set) {
// When every element is selected , Take it and arrange it , Then add the arrangement to list in .
if (el.size() == nums.length) {
ans.add(new ArrayList<>(el));
return;
}
// Standard backtracking
// duplicate removal , When two different numbers are selected in this position , And when these two numbers are equal , Produce repetition .
int pre = 1 << 31;
for (int i = 0; i < nums.length; i++) {
// The two elements currently selected are the same | This element has been selected , Then pass .
if (pre == nums[i] || set.contains(i)) continue;
// Standard backtracking
el.add(nums[i]);
set.add(i);
dfs(nums, el, ans, set);
set.remove(i);
el.remove(el.size() - 1);
// to update pre For the currently selected element , Prepare for the following choices .
pre = nums[i];
}
}
}
summary
1) Standard backtracking
2) By putting the elements together , Combined with pre and cur Whether the variables are equal to achieve the goal of de duplication .
reference
边栏推荐
- Bi-sql index
- Redis and jedis
- Bi SQL alias
- Cobalt strike installation tutorial
- Redis persistence
- An Chaoyun: "one cloud with multiple cores" supports the implementation of the national information innovation government cloud
- Assembly language (4) function transfer parameters
- Baidu voice synthesizes voice files and displays them on the website
- Ideas and examples of divide and conquer
- 百度语音合成语音文件并在网站中展示
猜你喜欢
Multi modal data can also be Mae? Berkeley & Google proposed m3ae to conduct Mae on image and text data! The optimal masking rate can reach 75%, significantly higher than 15% of Bert
动手学数据分析 数据建模和模型评估
Abnova丨BSG 单克隆抗体中英文说明
Using macro code to generate handwriting automatically in word or WPS
Use redis' sorted set to make weekly hot Reviews
多模态数据也能进行MAE?伯克利&谷歌提出M3AE,在图像和文本数据上进行MAE!最优掩蔽率可达75%,显著高于BERT的15%
How to store dataframe data in pandas into MySQL
Bi-sql between
Linux64Bit下安装MySQL5.6-不能修改root密码
Bi-sql - join
随机推荐
Reverse ordinal number by merge sort
音频PCM数据计算声音分贝值,实现简单VAD功能
广发期货安全吗?开户需要什么东西?
修身励学篇
Transformers 库的基本使用
Bi-sql index
Deoxyribonuclease I instructions in Chinese and English
JVM指令
胰蛋白酶中英文说明书
Assembly language (3) 16 bit assembly basic framework and addition and subtraction loop
SQL aggregate function handling null [easy to understand]
Redis persistence
监听 Markdown 文件并热更新 Next.js 页面
Application session coverage solutions with different ports on the same server
Poj3669 meteor shower (BFS pretreatment)
Linux64Bit下安装MySQL5.6-不能修改root密码
Tianshu night reading notes -- disassembly engine xde32
Reading notes at night -- deep into virtual function
Cloud development technology summit · public welfare programming challenge [hot registration]!
在两个有序数组中找到整体第K小的数可以做到O(log(Min(M,N)))