当前位置:网站首页>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
边栏推荐
猜你喜欢
![[leetcode] 11. Container with the most water](/img/40/8bb6506a29f8da797432fee50d3aad.png)
[leetcode] 11. Container with the most water

数组中关于sizeof()和strlen

Abnova BSG monoclonal antibody description in Chinese and English

Use redis' sorted set to make weekly hot Reviews

论文翻译 | RandLA-Net: Efficient Semantic Segmentation of Large-Scale Point Clouds

How to prepare for the last day of tomorrow's exam? Complete compilation of the introduction to the second building test site

Introduction to bi-sql wildcards
![全排列II[存在相同元素去重 + 标准回溯]](/img/d3/93ddb49e580be60be4f056f141b782.png)
全排列II[存在相同元素去重 + 标准回溯]

AutoCAD - two extension modes

TC对象结构和简称
随机推荐
Matlab rounding
修身励学篇
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 monoclonal antibody description in Chinese and English
天书夜读笔记——内存分页机制
Google browser console F12 how to set the Chinese / English switching method, we must see the last!!!
Using macro code to generate handwriting automatically in word or WPS
Use redis' sorted set to make weekly hot Reviews
Chinese and English instructions of trypsin
IPC mechanism
粉丝福利,JVM 手册(包含 PDF)
String common methods
Unity C# 网络学习(六)——FTP(一)
海河实验室创新联合体成立 GBASE成为首批创新联合体(信创)成员单位
通达信哪个开户更安全,更好点
CCNP的BGP部分笔记
【LeetCode】11、盛最多水的容器
Reverse ordinal number by merge sort
An Chaoyun: "one cloud with multiple cores" supports the implementation of the national information innovation government cloud
新一代可级联的以太网远程I/O数据采集模块