【LeetCode】Subsets II 解题报告

【题目】

Given a collection of integers that might contain duplicates, S, 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 S = [1,2,2], a solution is:

[
  [2],
  [1],
  [1,2,2],
  [2,2],
  [1,2],
  []
]

【解析】

看到这个题目,我立马想起了 【LeetCode】Combinations 解题报告 和 【LeetCode】Subsets 解题报告 。

题意:给定了数组,数组中的数有重复,要求出所有子集。

思路还是回溯。我沿着上道题的思路,分别回溯求长度为0~n的子集。并且在回溯过程中判断重复的子集。

public class Solution {
    List<List<Integer>> ans = new ArrayList<List<Integer>>();
    int[] num = null;

    public List<List<Integer>> subsetsWithDup(int[] num) {
        this.num = num;

        // Sort first!
        Arrays.sort(num);

        // Add the subsets of length from 0 to num.length by back tracking.
        for (int i = 0; i <= num.length; i++) {
            backTracking(new ArrayList<Integer>(), 0, i);
        }

        return ans;
    }

    public void backTracking(List<Integer> cur, int from, int cnt) {
        if (cur.size() == cnt) {
            // Find if cur is in the result.
            boolean hasSame = false;
            int k = ans.size() - 1;
            while (k >= 0 && ans.get(k).size() == cnt) {
                List<Integer> last = ans.get(k);
                int i = 0;
                for ( ; i < cnt; i++) {
                    if (last.get(i) != cur.get(i)) {
                        break;
                    }
                }
                if (i == cnt) {
                    hasSame = true;
                    break;
                }
                k--;
            }

            // If cur is not the same as last subset, add it to the result set.
            if (!hasSame) {
                List<Integer> list = new ArrayList<Integer>(cur);
                ans.add(list);
            }
        } else {
            // Go on back tracking.
            for (int i = from; i < num.length; i++) {
                cur.add(num[i]);
                backTracking(cur, i + 1, cnt);
                cur.remove(new Integer(num[i]));
            }
        }
    }
}
时间: 2024-10-11 17:26:12

【LeetCode】Subsets II 解题报告的相关文章

LeetCode: N-Queens II 解题报告

N-Queens II (LEVEL 4 难度级别,最高级5) Follow up for N-Queens problem. Now, instead outputting board configurations, return the total number of distinct solutions. 解答: 与第一题的解法是类似的,不同之处是,DFS的返回值是解的个数.我们每一次搜索本行8个位置时, 把8个位置的解都加在一起就行了.有的位置无解,它的返回值就是0咯. 另外注意BASE

LeetCode: Permutations II 解题报告

Permutations II Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example,[1,1,2] have the following unique permutations:[1,1,2], [1,2,1], and [2,1,1]. SOLUTION 1: 还是经典的递归模板.需要处理的情况是:我们先把Num排序,然

LeetCode: Combination Sum 解题报告

Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Question Solution Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The

LeetCode: Pascal&#39;s Triangle II 解题报告

Pascal's Triangle II Total Accepted: 19384 Total Submissions: 63446 My Submissions Question SolutionGiven an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to us

LeetCode: Unique Paths II 解题报告

Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty spac

[leetcode]Subsets II @ Python

原题地址:https://oj.leetcode.com/problems/subsets-ii/ 题意: Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicat

[LeetCode] Subsets II [32]

题目 Given a collection of integers that might contain duplicates, S, 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 S = [1,2,2], a solution

LeetCode: Subsets II [091]

[题目] Given a collection of integers that might contain duplicates, S, 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 S = [1,2,2], a solutio

[LeetCode]LRU Cache, 解题报告

题目 Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.