当前位置:网站首页>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
边栏推荐
- 胰蛋白酶中英文说明书
- The latest QQ wechat domain name anti red PHP program source code + forced jump to open
- 海河实验室创新联合体成立 GBASE成为首批创新联合体(信创)成员单位
- Huawei laptop, which grew against the trend in Q1, is leading PC into the era of "smart office"
- leetcode:2104. Subarray range and
- "One good programmer is worth five ordinary programmers!"
- Expectation and variance
- Bi-sql select into
- 15. several methods of thread synchronization
- This national day! Tencent cloud wecity will accompany you to travel and light up the city landmark
猜你喜欢

【LeetCode】11、盛最多水的容器

(CVPR 2020) Learning Object Bounding Boxes for 3D Instance Segmentation on Point Clouds

15. several methods of thread synchronization

Abnova a4gnt polyclonal antibody

Pbcms adding cyclic digital labels

pbcms添加循环数字标签

AUTOCAD——两种延伸方式

“一个优秀程序员可抵五个普通程序员!”

Using macro code to generate handwriting automatically in word or WPS

Application session coverage solutions with different ports on the same server
随机推荐
leetcode:2104. 子数组范围和
TC对象结构和简称
Bi-sql top
Tencent cloud wecity Hello 2022!
监听 Markdown 文件并热更新 Next.js 页面
Hands on data analysis data modeling and model evaluation
PMP考试“临门一脚”如何踢得漂亮?
在两个有序数组中找到整体第K小的数可以做到O(log(Min(M,N)))
‘distutils‘ has no attribute ‘version
Deoxyribonuclease I instructions in Chinese and English
数组中关于sizeof()和strlen
[leetcode] 11. Container with the most water
Abnova BSG monoclonal antibody description in Chinese and English
天书夜读笔记——深入虚函数virtual
Unity C# 网络学习(六)——FTP(一)
Abnova a4gnt polyclonal antibody
Deoxyribonuclease I instructions in Chinese and English
‘distutils‘ has no attribute ‘version
String common methods
期望与方差