ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

LeetCode 0090 Subsets II

2022-04-30 08:32:16  阅读:231  来源: 互联网

标签:subset Subsets nums into duplicate element II add LeetCode


原题传送门

1. 题目描述

2. Solution 1

1、思路分析
回溯法,类似Q78 Subsets,不过要注意去重。

2、代码实现

package Q0099.Q0090SubsetsII;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/*
    回溯法
 */
public class Solution1 {
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        List<List<Integer>> list = new ArrayList<>();
        Arrays.sort(nums);
        backtrack(list, new ArrayList<>(), nums, 0);
        return list;
    }

    private void backtrack(List<List<Integer>> list, ArrayList<Integer> curRes, int[] nums, int start) {
        list.add(new ArrayList<>(curRes));
        for (int i = start; i < nums.length; i++) {
            if (i > start && nums[i] == nums[i - 1]) continue;  // skip duplicates
            curRes.add(nums[i]);
            backtrack(list, curRes, nums, i + 1);
            curRes.remove(curRes.size() - 1);
        }
    }
}

3、复杂度分析
时间复杂度: O(n * 2^n)
空间复杂度: O(n/2 * 2^n) ~= O(n * 2^n)

3. Solution 2

1、思路分析
非递归
To solve this problem, it is helpful to first think how many subsets are there.
If there is no duplicate element, the answer is simply 2^n, where n is the number of elements.
This is because you have two choices for each element, either putting it into the subset or not.
So all subsets for this no-duplicate set can be easily constructed: num of subset

    (1 to 2^0) empty set is the first subset
    (2^0+1 to 2^1) add the first element into subset from (1)
    (2^1+1 to 2^2) add the second element into subset (1 to 2^1)
    (2^2+1 to 2^3) add the third element into subset (1 to 2^2)
    ....
    (2^(n-1)+1 to 2^n) add the nth element into subset(1 to 2^(n-1))

    Then how many subsets are there if there are duplicate elements? We can treat duplicate element as a spacial
    element. For example, if we have duplicate elements (5, 5), instead of treating them as two elements that are
    duplicate, we can treat it as one special element 5, but this element has more than two choices: you can either
    NOT put it into the subset, or put ONE 5 into the subset, or put TWO 5s into the subset. Therefore, we are
    given an array (a1, a2, a3, ..., an) with each of them appearing (k1, k2, k3, ..., kn) times, the number of
    subset is (k1+1)(k2+1)...(kn+1).
    We can easily see how to write down all the subsets similar to the approach above.

2、代码实现

package Q0099.Q0090SubsetsII;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Solution2 {
    /*
       method 1: 非递归
       To solve this problem, it is helpful to first think how many subsets are there.
       If there is no duplicate element, the answer is simply 2^n, where n is the number of elements.
       This is because you have two choices for each element, either putting it into the subset or not.
       So all subsets for this no-duplicate set can be easily constructed: num of subset

        (1 to 2^0) empty set is the first subset
        (2^0+1 to 2^1) add the first element into subset from (1)
        (2^1+1 to 2^2) add the second element into subset (1 to 2^1)
        (2^2+1 to 2^3) add the third element into subset (1 to 2^2)
        ....
        (2^(n-1)+1 to 2^n) add the nth element into subset(1 to 2^(n-1))

        Then how many subsets are there if there are duplicate elements? We can treat duplicate element as a spacial
        element. For example, if we have duplicate elements (5, 5), instead of treating them as two elements that are
        duplicate, we can treat it as one special element 5, but this element has more than two choices: you can either
        NOT put it into the subset, or put ONE 5 into the subset, or put TWO 5s into the subset. Therefore, we are
        given an array (a1, a2, a3, ..., an) with each of them appearing (k1, k2, k3, ..., kn) times, the number of
        subset is (k1+1)(k2+1)...(kn+1).
        We can easily see how to write down all the subsets similar to the approach above.
     */
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        List<Integer> empty = new ArrayList<>();
        result.add(empty);
        Arrays.sort(nums);

        for (int i = 0; i < nums.length; ) {
            int dupCount = 0;       // number of elements are the same
            while (((i + dupCount) < nums.length) && nums[i + dupCount] == nums[i]) dupCount++;
            int previousN = result.size();
            for (int j = 0; j < previousN; j++) {
                List<Integer> element = new ArrayList<>(result.get(j));
                for (int t = 0; t < dupCount; t++) {
                    element.add(nums[i]);
                    result.add(new ArrayList<>(element));
                }
            }
            i += dupCount;
        }
        return result;
    }
}

3、复杂度分析
时间复杂度:

标签:subset,Subsets,nums,into,duplicate,element,II,add,LeetCode
来源: https://www.cnblogs.com/junstat/p/16209374.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有