当前位置:网站首页>Subsets II of leetcode topic analysis
Subsets II of leetcode topic analysis
2022-06-23 08:39:00 【ruochen】
Given a collection of integers that might contain duplicates, nums, return all possible subsets.
Note:
Elements in a subset must be in non-descending order.
The solution set must not contain duplicate subsets.
For example,
If nums = 1,2,2, a solution is:
[
2,
1,
1,2,2,
2,2,
1,2,
[]
]
Use binary , There's no need for recursion . For every bit in the binary ,0 Indicates no choice ,1 Express choice . Then there are 2^n In this case .
Use HashSet, It can filter the repetition directly ( Sort the array first , The title also requires each list Non descending order ).
public List<List<Integer>> subsetsWithDup(int[] nums) {
if (nums == null) {
return null;
}
if (nums.length == 0) {
return new ArrayList<List<Integer>>();
}
Set<List<Integer>> set = new HashSet<List<Integer>>();
// The title requires each list Descending order of right and wrong , So you have to sort from small to large
Arrays.sort(nums);
// about n position , Yes 2^n In this case
for (int i = 0; i < Math.pow(2, nums.length); i++) {
List<Integer> list = new ArrayList<Integer>();
int tmp = i;
// For each case , Get the binary respectively 1 The number of
// 0 It means no choice ,1 For choice
for (int j = 0; j < nums.length; j++) {
int bit = tmp & 1;
tmp >>= 1;
if (bit == 1) {
list.add(nums[j]);
}
}
set.add(list);
}
return new ArrayList<List<Integer>>(set);
}边栏推荐
- [paper notes] catching both gray and black swans: open set supervised analog detection*
- Monitor the cache update of Eureka client
- usb peripheral 驱动 - configfs
- How to migrate x86 architecture applications to arm architecture at low cost
- 史上最污技术解读,60 个 IT 术语我居然秒懂了......
- 鸿蒙读取资源文件
- Third party payment in the second half: scuffle to symbiosis
- APM performance monitoring practice of jubasha app
- How to sort a dictionary by value or key?
- 5-rotating Daisy - rotating canvas and timer
猜你喜欢

Open source technology exchange batch stream integrated data synchronization engine Chunjun data restore DDL function module analysis

自组织映射神经网络(SOM)

给你的win10装一个wget

What are the PCB characteristics inspection items?

谈谈 @Autowired 的实现原理

最常用的5中流ETL模式

Summary of communication mode and detailed explanation of I2C drive
![Vulnhub | dc: 3 | [actual combat]](/img/97/e5ba86f2694fe1705c13c60484cff6.png)
Vulnhub | dc: 3 | [actual combat]

渲染效果图哪家好?2022最新实测(四)

【论文笔记】Catching Both Gray and Black Swans: Open-set Supervised Anomaly Detection*
随机推荐
Moodle e-learning platform fixes the session hijacking error that leads to pre authorized rce
坑爹的“敬业福”:支付宝春晚红包技术大爆发
vector的深度剖析及模拟实现
Focus! Ten minutes to master Newton convex optimization
目标检测中的多尺度特征结合方式
5-rotating Daisy - rotating canvas and timer
ArcLayoutView: 一个弧形布局的实现
What is a dedicated server line
Go data types (II) overview of data types supported by go and Boolean types
USB peripheral driver - configfs
Set interface and set sub implementation classes
Kernel log debugging method
Structure and usage of transform
Can portals be the next decentraland?
Monitor the cache update of Eureka client
训练后的随机森林模型导出和加载
Open source stealing malware mercurial found in the field for "educational purposes"
Pyspark on HPC (Continued): reasonable partition processing and consolidated output of a single file
Qualcomm 9x07 two startup modes
Use of tensorboard