# 90 Subsets II

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],
[]
]

1. 对数组进行排序
2. 从前往后依次遍历数组中的元素
3. 遍历列表中现存的每个子集，使它们分别加上当前遍历到的数组元素，形成一个个新的子集
4. 列表中的初始子集就是数组遍历到的第一个元素

public class Solution {
public List<List<Integer>> subsetsWithDup(int[] nums) {
List<List<Integer>> res = new ArrayList();
if(nums == null || nums.length == 0)
return res;
Arrays.sort(nums);
int start = 0;
int n = 0; // 记录上一轮新添加的子集个数
for(int i = 0; i < nums.length; i ++) {
int size = res.size();
if(i > 0 && nums[i - 1] == nums[i])
start = size - n;
else
start = 0;
n = 0;
for(int j = start; j < size; j ++) {
List<Integer> elem = new ArrayList(res.get(j));
n ++;
}
}
return res;
}
}
19 / 19 test cases passed.
Status: Accepted
Runtime: 3 ms

public class Solution {
public List<List<Integer>> subsetsWithDup(int[] num) {
List<List<Integer>> res = new ArrayList();
if(num==null || num.length==0)
return res;
Arrays.sort(num);
int start = 0;
for(int i=0;i<num.length;i++)
{
int size = res.size();
for(int j=start;j<size;j++)
{
List<Integer> newItem = new ArrayList<Integer>(res.get(j));
}
if(i<num.length-1 && num[i]==num[i+1])
{
start = size;
}
else
{
start = 0;
}
}
return res;
}
}
19 / 19 test cases passed.
Status: Accepted
Runtime: 3 ms

#### [LeetCode 90] Subsets II

2015-03-28 00:32:07

#### LeetCode90:Subsets II

2015-07-08 11:12:56

#### 90. Subsets II Leetcode Python

2015-01-15 06:03:55

#### LeetCode 90 Subsets II--In C++

2016-06-04 16:01:54

#### leetCode 90.Subsets II（子集II） 解题思路和方法

2015-07-25 09:54:00

#### leetcode解题之 77. Combinations&78. Subsets&90. Subsets II java 版（求所有子集）

2017-04-08 19:36:52

#### Leetcode 90. Subsets II 子集2 解题报告

2016-07-05 20:51:34

#### LeetCode 90：Subsets II

2016-01-25 17:09:43

#### Leetcode 90 Subsets II (打印不重复的子集）

2016-11-01 18:58:14

#### LeetCode 90. Subsets II（子集）

2016-05-23 00:34:52