90. Subsets II

题目:

Given a collection of integers that might contain duplicates, nums, return all possible subsets.

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

链接:https://leetcode.com/problems/subsets-ii/#/description

4/22/2017

算法班

37%, 3ms

与第78题只有一句话的区别,就是第21行去重

 1 public class Solution {
 2
 3     public List<List<Integer>> subsetsWithDup(int[] nums) {
 4         // write your code here
 5         List<List<Integer>> ret = new ArrayList<>();
 6
 7         Arrays.sort(nums);
 8
 9         helper(ret, new ArrayList<Integer>(), nums, 0);
10
11         return ret;
12     }
13
14     private void helper(List<List<Integer>> ret,
15                         ArrayList<Integer> subset,
16                         int[] nums,
17                         int startIndex) {
18         ret.add(new ArrayList<Integer>(subset));
19
20         for (int i = startIndex; i < nums.length; i++) {
21             if (i > startIndex && nums[i] == nums[i - 1]) continue;
22             subset.add(nums[i]);
23             helper(ret, subset, nums, i + 1);
24             subset.remove(subset.size() - 1);
25         }
26     }
27 }

别人iterative的解释

https://discuss.leetcode.com/topic/4661/c-solution-and-explanation

更多讨论:

https://discuss.leetcode.com/category/98/subsets-ii

时间: 2024-10-10 23:37:41

90. Subsets II的相关文章

LeetCode --- 90. Subsets II

题目链接: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]

【LeetCode】90.Subsets II

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]

LeetCode Problem 90. Subsets II

python solution 123456789101112131415161718192021222324252627 class (object): def subsetsWithDup(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ """ 思路整理:DFS recursion 1)对nums进行排序以避免nums中重复元素

90. Subsets II (Recursion, DP)

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 solutio

leetcode 90 Subsets II ----- java

Given a collection of integers that might contain duplicates, nums, return all possible subsets. Note: 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], [] ] 这道题就是Sub

LeetCode 90. Subsets II (子集合之二)

Given a collection of integers that might contain duplicates, nums, return all possible subsets. Note: 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], [] ] 题目标签:Arr

leetCode 90.Subsets II(子集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 soluti

[leedcode 90] 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 solutio

[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. Input: [1,2,2] Output: [ [2], [1], [1,2,2], [2,2], [1,2], [] ] 题意: 是的[leetcode