Java for LeetCode 090 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 solution is:

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

解题思路一:

偷懒做法,将Java for LeetCode 078 Subsets中的List换为Set即可通过测试,JAVA实现如下:

public List<List<Integer>> subsetsWithDup(int[] nums) {
	    Set<List<Integer>> list = new HashSet<List<Integer>>();
	    list.add(new ArrayList<Integer>());
	    Arrays.sort(nums);
	    for(int i=1;i<=nums.length;i++)
	        dfs(list, nums.length, i, 0,nums,-1);
	    return new ArrayList(list);
	}

	static List<Integer> alist = new ArrayList<Integer>();

	static void dfs(Set<List<Integer>> list, int n, int k, int depth,int[] nums,int last) {
	    if (depth >= k) {
	        list.add(new ArrayList<Integer>(alist));
	        return;
	    }
	    for (int i = last+1; i <= n-k+depth; i++) {
	        alist.add(nums[i]);
	        dfs(list, n, k, depth + 1,nums,i);
	        alist.remove(alist.size() - 1);
	    }
	}
时间: 2024-12-05 04:56:58

Java for LeetCode 090 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】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], a so

[LeetCode][JavaScript]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 dfs Subsets II

Subsets II Total Accepted: 19243 Total Submissions: 71148My Submissions 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 no

【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 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

[LeetCode] 90.Subsets II tag: backtracking

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]题解(python):090 Subsets II

题目来源 https://leetcode.com/problems/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 su

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