您的位置:首页 > 其它

90 Subsets II

2015-11-18 21:21 302 查看
题目链接:https://leetcode.com/problems/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],
[]
]


解题思路:

这是一道经典的 NP问题。解决办法与 78 Subsets 非常相似。

基本步骤都是:

1. 对数组进行排序

2. 从前往后依次遍历数组中的元素

3. 遍历列表中现存的每个子集,使它们分别加上当前遍历到的数组元素,形成一个个新的子集

4. 列表中的初始子集就是数组遍历到的第一个元素

对这道题来说,不同点在于可能存在重复的元素,使得新生成的子集与原有子集相同。

解决方法:

只对上一轮添加元素后生成的那些新子集添加当前元素(当前元素与上次添加的元素相同)。

因此,需要记录上一轮新添加的子集个数。

或者,在每一轮结束后,若遇到下一个元素与当前元素相同的情况,就把下一轮遍历子集的起始位置设为当前轮未添加新子集前,列表中的子集个数。

附上大神的解答:/article/1378227.html

注:

其实这题与 89 Gray Code 也比较像,都是在已有元素的基础上添加东西,新成新的元素。真应该好好总结!

代码实现:

public class Solution {
public List<List<Integer>> subsetsWithDup(int[] nums) {
List<List<Integer>> res = new ArrayList();
res.add(new ArrayList<Integer>());
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));
elem.add(nums[i]);
res.add(elem);
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();
res.add(new ArrayList<Integer>());
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));
newItem.add(num[i]);
res.add(newItem);
}
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: