90 Subsets II

这道LeetCode题目90,子集II(Subsets II),是一个NP问题,与78题子集类似但涉及重复元素。解题策略包括排序数组、遍历元素并递归生成子集,重点在于处理重复元素,避免生成重复子集。文章提供了解题思路和大神的代码链接,同时指出该题与89题灰码问题在生成新元素上有相似之处。
摘要由CSDN通过智能技术生成

题目链接: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. 列表中的初始子集就是数组遍历到的第一个元素

对这道题来说,不同点在于可能存在重复的元素,使得新生成的子集与原有子集相同。
解决方法:
只对上一轮添加元素后生成的那些新子集添加当前元素(当前元素与上次添加的元素相同)。
因此,需要记录上一轮新添加的子集个数。
或者,在每一轮结束后,若遇到下一个元素与当前元素相同的情况,就把下一轮遍历子集的起始位置设为当前轮未添加新子集前,列表中的子集个数。

附上大神的解答:http://blog.csdn.net/linhuanmars/article/details/24613193

注:
其实这题与 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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值