当前位置:网站首页>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
边栏推荐
- Chinese and English instructions of Papain
- Elastase instructions in Chinese and English
- ICML2022 | 用神经控制微分方程建立反事实结果的连续时间模型
- pbcms添加循环数字标签
- 多模态数据也能进行MAE?伯克利&谷歌提出M3AE,在图像和文本数据上进行MAE!最优掩蔽率可达75%,显著高于BERT的15%
- matlab 取整
- Google browser console F12 how to set the Chinese / English switching method, we must see the last!!!
- 修身励学篇
- Basic use of transformers Library
- The latest QQ wechat domain name anti red PHP program source code + forced jump to open
猜你喜欢
随机推荐
Merge sort template & understanding
带马尔科夫切换的正向随机微分方程数值格式模拟
Hands on data analysis data modeling and model evaluation
Tencent moved!
Some Modest Advice for Graduate Students - by Stephen C. Stearns, Ph.D.
Fan benefits, JVM manual (including PDF)
Basic use of transformers Library
WinXP内核驱动调试
PS5连接OPPO K9电视不支持2160P/4K
Abnova BSG monoclonal antibody description in Chinese and English
Reading notes at night -- deep into virtual function
Redis and jedis
Bi-sql index
How to prepare for the last day of tomorrow's exam? Complete compilation of the introduction to the second building test site
The latest QQ wechat domain name anti red PHP program source code + forced jump to open
SQL aggregate function handling null [easy to understand]
Bi SQL constraints
Bi-sql create
(CVPR 2020) Learning Object Bounding Boxes for 3D Instance Segmentation on Point Clouds
Pbcms adding cyclic digital labels


![搜索二维矩阵[二分巧用 + 记录不同于插入二分的解法]](/img/c9/afc03afd477bbfdd3c0dc54bacfd2d.png)






