当前位置:网站首页>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
边栏推荐
- JVM third talk -- JVM performance tuning practice and high-frequency interview question record
- Why is all (()) true and any (()) false?
- EasyCVR平台通过接口编辑通道出现报错“ID不能为空”,是什么原因?
- 检查命名空间和类
- 《2022中国信创生态市场研究及选型评估报告》发布 华云数据入选信创IT基础设施主流厂商!
- 使用Jmeter虚拟化table失败
- nacos -分布式事务-Seata** linux安装jdk ,mysql5.7启动nacos配置ideal 调用接口配合 (保姆级细节教程)
- Isprs2022 / Cloud Detection: Cloud Detection with Boundary nets Boundary Networks Based Cloud Detection
- 怎么选择外盘期货平台最正规安全?
- Redis Foundation
猜你喜欢
Zabbix
隐私计算助力数据的安全流通与共享
2022新版PMP考试有哪些变化?
修复漏洞 - mysql 、es
To solve the stubborn problem of Lake + warehouse hybrid architecture, xinghuan Technology launched an independent and controllable cloud native Lake warehouse integrated platform
About Estimation with Cross-Validation
星环科技数据安全管理平台 Defensor重磅发布
Nanjing University: Discussion on the training program of digital talents in the new era
Why is all (()) true and any (()) false?
[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
随机推荐
To solve the stubborn problem of Lake + warehouse hybrid architecture, xinghuan Technology launched an independent and controllable cloud native Lake warehouse integrated platform
Tencent music launched its new product "quyimai", which provides music commercial copyright authorization
集群部署如何解决海量视频接入与大并发需求?
Elk log analysis system
神经网络自我认知模型
OpenShift常用管理命令杂记
检查命名空间和类
从XML架构生成类
About Estimation with Cross-Validation
GFS distributed file system
Cmake tutorial Step2 (add Library)
使用QT遍历Json文档及搜索子对象
Penetrate the whole intranet through socks agent
Leetcode daily question: the first unique character in the string
Can communication of nano
Matlab built-in function how different colors, matlab subsection function different colors drawing
第十一届中国云计算标准和应用大会 | 云计算国家标准及白皮书系列发布 华云数据全面参与编制
ISPRS2022/雲檢測:Cloud detection with boundary nets基於邊界網的雲檢測
Anaconda中配置PyTorch环境——win10系统(小白包会)
南京大学:新时代数字化人才培养方案探讨