90. Subsets II (M)

探讨了在存在重复元素的情况下生成数组的所有可能子集(幂集)的问题,提出了解决方案以避免重复子集,并提供了递归和迭代两种代码实现。

Subsets II (M)

Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).

Note: The solution set must not contain duplicate subsets.

Example:

Input: [1,2,2]
Output:
[
  [2],
  [1],
  [1,2,2],
  [2,2],
  [1,2],
  []
]

题意

求一个数组的幂集(即所有子集,包括全集和空集,构成的集族),数组中可以有重复元素,不允许有重复的子集。

思路

78. Subsets 相比,数组中多了重复元素,在使用回溯法时就需要判断要不要把当前元素加入到结果集中。

对于数组[2, 2]来说,如果按照先前方法递归,会得到以下4组子集(空格表示递归时没有选取对应位置的元素):

1234
[2, 2][2, ][ , 2][ , ]

但可以看到这4组中,第2、3组实际上是一个集合,关键在于对第二个2是否选取的判断。

为了避免重复集合,可以采取以下措施:记 nums[index] 为当前待选取的元素,如果 nums[index] == nums[index - 1],则当且仅当 nums[index - 1] 已经在列表中,才需要将 nums[index] 加入结果集并继续递归,否则不将 nums[index] 加入结果集。以数组[2, 2, 2]为例实行上面的做法:

index = 2
index = 1
index = 0
加入第一个2
不加入第一个2
加入第二个2
不加入第二个2
不加入第二个2
加入第三个2
不加入第三个2
不加入第三个2
不加入第三个2
{2, 2, 2}
{2, 2}
{2}
{}
{2, 2}
{2}
{}
{2}
{}
{}

代码实现 - 递归

class Solution {
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        List<List<Integer>> ans = new ArrayList<>();
        Arrays.sort(nums);								// 先排序便于处理
        dfs(nums, 0, new ArrayList<>(), ans, false);
        return ans;
    }

    private void dfs(int[] nums, int index, List<Integer> list, List<List<Integer>> ans, boolean preAdded) {
        if (index == nums.length) {
            ans.add(new ArrayList<>(list));
            return;
        }

        // 如果与前一个元素相同则需要进行特殊处理,否则按正常做法分两条路递归
        if (index > 0 && nums[index] == nums[index - 1]) {
            // 只有当前一个元素已经加入,才需要加入当前元素
            if (preAdded) {
                list.add(nums[index]);
                dfs(nums, index + 1, list, ans, true);
                list.remove(list.size() - 1);
            }
            dfs(nums, index + 1, list, ans, false);
        } else {
            list.add(nums[index]);
            dfs(nums, index + 1, list, ans, true);
            list.remove(list.size() - 1);
            dfs(nums, index + 1, list, ans, false);
        }
    }
}

代码实现 - 迭代

class Solution {
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        List<List<Integer>> ans = new ArrayList<>();
        Arrays.sort(nums);
        ans.add(new ArrayList<>());
        
        int preSize = 0;		// 记录了结果集中添加了上一元素的第一个列表的下标
        for (int i = 0; i < nums.length; i++) {
            int size = ans.size();
            // 若为重复元素,则只在添加了上一元素的列表后添加当前元素
            int index = i > 0 && nums[i] == nums[i - 1] ? preSize : 0;
            for (int j = index; j < size; j++) {
                List<Integer> temp = new ArrayList<>(ans.get(j));
                temp.add(nums[i]);
                ans.add(temp);
            }
            preSize = size;
        }
        
        return ans;
    }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值