当前位置:网站首页>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
边栏推荐
- Reading notes at night -- deep into virtual function
- (CVPR 2020) Learning Object Bounding Boxes for 3D Instance Segmentation on Point Clouds
- Bi-sql - different join
- How to prepare for the last day of tomorrow's exam? Complete compilation of the introduction to the second building test site
- void* 指针
- 字符串常用方法
- Expectation and variance
- 在两个有序数组中找到整体第K小的数可以做到O(log(Min(M,N)))
- Abnova 5-methylcytosine polyclonal antibody
- Deoxyribonuclease I instructions in Chinese and English
猜你喜欢

Abnova丨CSV 磁珠中英文说明

15. several methods of thread synchronization

Linux64Bit下安装MySQL5.6-不能修改root密码

Abnova 5-methylcytosine polyclonal antibody

Abnova CSV magnetic beads description in Chinese and English

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

pbcms添加循环数字标签

Using macro code to generate handwriting automatically in word or WPS

谷歌浏览器控制台 f12怎么设置成中文/英文 切换方法,一定要看到最后!!!

第04天-文件IO
随机推荐
数组中关于sizeof()和strlen
IPC机制
TC对象结构和简称
Assembly language (3) 16 bit assembly basic framework and addition and subtraction loop
Why does Dell always refuse to push the ultra-thin commercial notebook to the extreme?
Hands on data analysis data modeling and model evaluation
Tencent cloud wecity Hello 2022!
Tencent cloud wecity Industry joint collaborative innovation to celebrate the New Year!
天书夜读笔记——深入虚函数virtual
Program.launch(xxx)打开文件
Ideas and examples of divide and conquer
Bi SQL constraints
胰蛋白酶中英文说明书
Introduction to bi-sql wildcards
Bi SQL drop & alter
Chinese and English instructions of Papain
国内炒股开户正规安全的具体名单
谷歌浏览器控制台 f12怎么设置成中文/英文 切换方法,一定要看到最后!!!
Cloud development technology summit · public welfare programming challenge [hot registration]!
高考之后,必然会出现以下四种情况: