当前位置:网站首页>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
边栏推荐
- 全排列II[存在相同元素去重 + 标准回溯]
- Expectation and variance
- Powerbi - for you who are learning
- Introduction to bi-sql wildcards
- Excel Chinese character to pinyin "suggestions collection"
- IPC mechanism
- AssertionError: CUDA unavailable, invalid device 0 requested
- Reverse ordinal number by merge sort
- 结合实操带你吃透Redis持久化
- Bi SQL alias
猜你喜欢
随机推荐
Tianshu night reading notes -- memory paging mechanism
AssertionError: CUDA unavailable, invalid device 0 requested
Baidu voice synthesizes voice files and displays them on the website
Abnova丨CSV 磁珠中英文说明
屡获大奖的界面控件开发包DevExpress v22.1官宣发布
Pbcms adding cyclic digital labels
1. package your own scaffold 2 Create code module
数组中关于sizeof()和strlen
‘distutils‘ has no attribute ‘version
Bi-sql - different join
Bi-sql top
实验5 8254定时/计数器应用实验【微机原理】【实验】
mpls 笔记 part 1
Bi-sql Union
Bi skill - judge 0 and null
Icml2022 | establishing a continuous time model of counterfactual results using neural control differential equations
AssertionError: CUDA unavailable, invalid device 0 requested
论文翻译 | RandLA-Net: Efficient Semantic Segmentation of Large-Scale Point Clouds
Abnova丨5-甲基胞嘧啶多克隆抗体中英文说明
Bi SQL alias







