当前位置:网站首页>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);
}边栏推荐
- Part of speech family
- XSS via host header
- Introduction to typescript and basic types of variable definitions
- Deep learning ----- different methods to realize vgg16
- Easygbs cannot play video streams in webrtc format. What is the reason?
- Easycvr accesses the website through the domain name. How to solve the problem that the video cannot be viewed back?
- 4-绘制椭圆、使用定时器
- How to solve the problem that flv video stream cannot be played and TS file generation fails due to packet loss?
- [paper notes] catching both gray and black swans: open set supervised analog detection*
- 你有一串代码,但是不支持低版本Go时;判断Go版本号,您值得拥有!
猜你喜欢

Which one is better for rendering renderings? 2022 latest measured data (IV)

Use of tensorboard

给你的win10装一个wget

Why use growth neural gas network (GNG)?

Hongmeng reads the resource file

Object. Defineproperty() and data broker

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

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

Basic use of check boxes and implementation of select all and invert selection functions

Deep learning ----- different methods to realize vgg16
随机推荐
How to start Jupiter notebook in CONDA virtual environment
After easynvr video is enabled, no video file is generated. How to solve this problem?
kernel log调试方法
Generate code 39 extension code in batch through Excel file
When easynvr service is started, video cannot be played due to anti-virus software interception. How to deal with it?
6-闪耀的激光-CALayer 的应用
Moodle e-learning platform fixes the session hijacking error that leads to pre authorized rce
Top 25 most popular articles on vivo Internet technology in 2021
Only 187 bytes of desktop dream code
The rtsp/onvif protocol video platform easynvr startup service reports an error "service not found". How to solve it?
Single core driver module
ArcLayoutView: 一个弧形布局的实现
观察者模式
Assembly (receive several n-digit decimal values (0~65535) from the keyboard and display their sum in different base numbers.)
Leetcode topic analysis set matrix zeroes
测试-- 自动化测试selenium(关于API)
【论文笔记】Catching Both Gray and Black Swans: Open-set Supervised Anomaly Detection*
Go language basic conditional statement if
How to restore visualizations and dashboards after kibana rebuilds the index
Paper reading [quovadis, action recognition? A new model and the dynamics dataset]