当前位置:网站首页>Whether to take a duplicate subset with duplicate elements [how to take a subset? How to remove duplicates?]
Whether to take a duplicate subset with duplicate elements [how to take a subset? How to remove duplicates?]
2022-07-05 18:04:00 【REN_ Linsen】
Take a non repeating subset with repeating elements
Preface
Finding the key problem is a person's core ability , For example, take the non repeating subset with repeating elements , Two key issues are , First of all , How to get subsets ? second , How to go heavy ?
For how to get subsets , This paper sorts out three ways ;
For how to remove weight , This paper sorts out two ways .
One 、 A subset of II

Two 、 Take a subset + duplicate removal
package everyday.bitManipulation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class SubsetsWithDup {
/* target: Non repeating subset . How to get subsets ? Constantly add an element . How to go heavy ? Sort +pre+ Record the location of the last group added . */
public List<List<Integer>> subsetsWithDup(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> subsets = new ArrayList<>();
subsets.add(new ArrayList<>());
int pre = 1 << 31, begin = 0;
for (int num : nums) {
int i = 0, size = subsets.size();
if (pre == num) i = begin;
while (i < size) {
List<Integer> el = new ArrayList<>(subsets.get(i++));
el.add(num);
subsets.add(el);
}
begin = size;
pre = num;
}
return subsets;
}
/* Above, we have explored two core issues , And I finished the problem solution in my own way . Take a look at how the official advanced practice , That is, if the official solves the following two problems , 1- How to get subsets ? Binary enumeration + Take this bit as 1 Collection elements for , Time complexity O(2^n^ * n) 2- How to go heavy ? Sort + The two are equal + The previous one is in the subset */
public List<List<Integer>> subsetsWithDup2(int[] nums) {
int n = nums.length;
Arrays.sort(nums);
List<List<Integer>> subsets = new ArrayList<>();
List<Integer> subset = new ArrayList<>();
for (int mask = 0; mask < 1 << n; mask++) {
// Take the element by every bit of binary .
boolean isValid = true;
for (int i = 0; i < n; i++) {
// Determine whether the element is in the current subset .
if ((mask & 1 << i) != 0) {
if (i > 0 && nums[i] == nums[i - 1] && (mask >> i - 1 & 1) == 0) {
// Duplicate occurs
isValid = false;
break;
}
// No repetition
subset.add(nums[i]);
}
}
// Repeat, don't repeat and half cut the subset , If you don't repeat, you have to .
if (isValid) subsets.add(new ArrayList<>(subset));
// Clear subset
subset.clear();
}
return subsets;
}
/* Above, we have explored two core issues , And I finished the problem solution in my own way . Take a look at how the official advanced practice , That is, if the official solves the following two problems , 1- How to get subsets ? choice or Do not select the current element . 2- How to go heavy ? Sort + The previous and currently selected elements do not appear at the same time -> Go beyond this subset . */
public List<List<Integer>> subsetsWithDup3(int[] nums) {
int n = nums.length;
Arrays.sort(nums);
List<List<Integer>> subsets = new ArrayList<>();
List<Integer> subset = new ArrayList<>();
dfs(nums, 0, false, subset, subsets);
return subsets;
}
// choose or No election , Consider n Elements , So it is (1 + 1)^n^
private void dfs(int[] nums, int cur, boolean isChoosePre, List<Integer> subset, List<List<Integer>> subsets) {
if (cur == nums.length) {
subsets.add(new ArrayList<>(subset));
return;
}
// choice or Do not select the current element .
// Do not select the current element , Shit no
dfs(nums, cur + 1, false, subset, subsets);
// Select the current element , When cur - 1 But I have no choice , And they are equal , There must be duplicate subsets , Skip over .
if (cur > 0 && nums[cur] == nums[cur - 1] && !isChoosePre) return;
// The front one chose or The front and back are not equal , Add elements normally , Generating subsets .
// Standard backtracking
subset.add(nums[cur]);
dfs(nums, cur + 1, true, subset, subsets);
subset.remove(subset.size() - 1);
}
}
summary
1) Exercise yourself to find core problems / Ability to ask key questions .
2) Three ways to generate subsets , Add elements circularly | Binary enumeration | dfs Select or deselect elements
3) Two ways of judging subset repetition , Sort + Whether the front and back are equal | Sort + Whether the front and back are equal + Whether the front is selected
reference
边栏推荐
猜你喜欢

EPM related

第十一届中国云计算标准和应用大会 | 华云数据成为全国信标委云计算标准工作组云迁移专题组副组长单位副组长单位

星环科技重磅推出数据要素流通平台Transwarp Navier,助力企业实现隐私保护下的数据安全流通与协作

Sophon KG升级3.1:打破数据间壁垒,解放企业生产力

pytorch yolov5 训练自定义数据

LeetCode每日一题:合并两个有序数组

GFS分布式文件系统
![Maximum artificial island [how to make all nodes of a connected component record the total number of nodes? + number the connected component]](/img/8b/a60fc36115580f018445e4c2a28a9d.png)
Maximum artificial island [how to make all nodes of a connected component record the total number of nodes? + number the connected component]

Cmake tutorial Step4 (installation and testing)

模拟百囚徒问题
随机推荐
Why is all (()) true and any (()) false?
How awesome is the architecture of "12306"?
Zabbix
钉钉开放平台小程序API的缓存接口都有哪些内容?
Gimp 2.10 tutorial "suggestions collection"
OpenShift常用管理命令杂记
PMP认证需具备哪些条件啊?费用多少啊?
pytorch yolov5 训练自定义数据
寻找第k小元素 前k小元素 select_k
ISPRS2020/云检测:Transferring deep learning models for cloud detection between Landsat-8 and Proba-V
Disorganized series
「运维有小邓」用于云应用程序的单点登录解决方案
Introduction to VC programming on "suggestions collection"
"Xiaodeng in operation and maintenance" is a single sign on solution for cloud applications
吴恩达团队2022机器学习课程,来啦
吳恩達團隊2022機器學習課程,來啦
Career advancement Guide: recommended books for people in big factories
Daily exercise: a series of dates
Use of print function in MATLAB
消除`if()else{ }`写法