当前位置:网站首页>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
边栏推荐
- 记一次使用Windbg分析内存“泄漏”的案例
- Tencent music launched its new product "quyimai", which provides music commercial copyright authorization
- [BeanShell] there are many ways to write data locally
- Binder开辟线程数过多导致主线程ANR异常
- 含重复元素取不重复子集[如何取子集?如何去重?]
- VC编程入门浅谈「建议收藏」
- 钉钉开放平台小程序API的缓存接口都有哪些内容?
- [JMeter] advanced writing method of JMeter script: all variables, parameters (parameters can be configured by Jenkins), functions, etc. in the interface automation script realize the complete business
- Sophon AutoCV:助力AI工业化生产,实现视觉智能感知
- Sophon kg upgrade 3.1: break down barriers between data and liberate enterprise productivity
猜你喜欢

EPM相关
![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]

Zabbix
![[JMeter] advanced writing method of JMeter script: all variables, parameters (parameters can be configured by Jenkins), functions, etc. in the interface automation script realize the complete business](/img/a6/aa0b8d30913dc64f3c0cd891528c40.png)
[JMeter] advanced writing method of JMeter script: all variables, parameters (parameters can be configured by Jenkins), functions, etc. in the interface automation script realize the complete business

小林coding的内存管理章节

图片数据不够?我做了一个免费的图像增强软件

使用Jmeter虚拟化table失败

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

To solve the stubborn problem of Lake + warehouse hybrid architecture, xinghuan Technology launched an independent and controllable cloud native Lake warehouse integrated platform

南京大学:新时代数字化人才培养方案探讨
随机推荐
[performance test] full link voltage test
第十届全球云计算大会 | 华云数据荣获“2013-2022十周年特别贡献奖”
Memory management chapter of Kobayashi coding
Failed to virtualize table with JMeter
JVM第三话 -- JVM性能调优实战和高频面试题记录
PMP认证需具备哪些条件啊?费用多少啊?
LeetCode笔记:Weekly Contest 300
Introduction to VC programming on "suggestions collection"
Ten capabilities that cyber threat analysts should have
Sophon base 3.1 launched mlops function to provide wings for the operation of enterprise AI capabilities
Record a case of using WinDbg to analyze memory "leakage"
mybash
Wu Enda team 2022 machine learning course, coming
Xiaobai getting started with NAS - quick building private cloud tutorial series (I) [easy to understand]
钉钉开放平台小程序API的缓存接口都有哪些内容?
【PaddleClas】常用命令
Generate XML schema from class
破解湖+仓混合架构顽疾,星环科技推出自主可控云原生湖仓一体平台
Teamcenter 消息注册前操作或後操作
EPM相关