LeetCode 90. Subsets II

题目

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

这道题和LeetCode 78. Subsets很像,唯一加了一个条件就是输入的数字可能含有重复的,因此生成的结果就不应该包含重复的数字。

如果采用backtracking来做,其实跟78基本上是一样的,只是需要先排个序,然后在遍历nums时把重复的数字跳过即可。时间复杂度跟之前应该还是一样的O(n * 2^n)。

Runtime: 2 ms, faster than 41.86% of Java online submissions for Subsets II.

Memory Usage: 39.9 MB, less than 40.45% of Java online submissions for Subsets II.

class Solution {
    public void backtrack(List<List<Integer>> result, List<Integer> temp, 
                         int[] nums, int start) {
        result.add(new LinkedList<>(temp));
        for (int i = start; i < nums.length; i++) {
            if (i > start && nums[i] == nums[i - 1]) {
                continue;
            }
            temp.add(nums[i]);
            backtrack(result, temp, nums, i + 1);
            temp.remove(temp.size() - 1);
        }
    }
    
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        List<List<Integer>> result = new LinkedList<>();
        Arrays.sort(nums);
        backtrack(result, new LinkedList<>(), nums, 0);
        return result;
    }
}

根据78题的最简单的那个思路(每次对之前的所有结果加入新的数字),也可以稍微修改一下写出来。但是不能一遇到重复的数字就立马跳过,而需要仔细观察整个生成数字的过程。https://leetcode.wang/leetCode-90-SubsetsII.html 这里的思路讲解的很清楚,借鉴一下这个图:

如果要从[1, 2, 2]之中生成subset,如果每次都在所有已经存在的结果之后加入新的数字,那么当遍历到重复数字的时候, 就会发现重复的解是由上一步之前就已经存在的解生成的。于是,我们就可以通过记录新的解的位置,如果遇到重复的数字,就跳过旧的解,只对新的解进行增加。代码整体上和78的写法差不多,就是加了句判断重复的以及记录新的解的起始位置。

Runtime: 5 ms, faster than 12.55% of Java online submissions for Subsets II.

Memory Usage: 41.3 MB, less than 9.86% of Java online submissions for Subsets II.

class Solution {
    
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        List<List<Integer>> result = new LinkedList<>();
        result.add(new LinkedList<>());
        Arrays.sort(nums);
        int newStart = 0;
        for (int i = 0; i < nums.length; i++) {
            int size = result.size();
            for (int j = 0; j < size; j++) {
                if (i > 0 && nums[i] == nums[i - 1] && j < newStart) {
                    continue;
                }
                List<Integer> temp = new LinkedList<>(result.get(j));
                temp.add(nums[i]);
                result.add(temp);
            }
            newStart = size;
        }
        return result;
    }
}

这道题还有另外一种思路,就是记录重复数字的出现次数,对已经存在的解,后面加上1, 2, ..., n个这个数字。因为每次外层for循环的时候需要再判断是否有重复的数字,所以刚开始把for改成了while自己把自己绕进去了……后来才意识到并不需要这么操作,每次内层的while的时候已经在increment i了,在判断重复的时候采用nums[i + 1] == nums[i],这样就会在最后一个重复数字的位置停下,不管有没有进入那个while,最后都要i++,所以其实就是个简单的for循环。

Runtime: 8 ms, faster than 9.93% of Java online submissions for Subsets II.

Memory Usage: 41.6 MB, less than 5.05% of Java online submissions for Subsets II.

class Solution {
    
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        List<List<Integer>> result = new LinkedList<>();
        result.add(new LinkedList<>());
        Arrays.sort(nums);
        for (int i = 0; i < nums.length; i++) {
            int dupCount = 1;
            while (i + 1 < nums.length && nums[i] == nums[i + 1]) {
                dupCount++;
                i++;
            }
            int size = result.size();
            for (int j = 0; j < size; j++) {
                List<Integer> temp = new LinkedList<>(result.get(j));
                int count = dupCount;
                for (int k = 0; k < dupCount; k++) {
                    temp.add(nums[i]);
                    result.add(new LinkedList<>(temp));
                }
            }
        }
        return result;
    }
}

以及据说这道题也能用位操作做,但是懒得看了,放弃。上文中贴的链接里有。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值