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

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

Subscribe to see which companies asked this question

这题有重复元素,但本质上,跟上题很类似,也可以用类似的方法处理:

class Solution {
public:
	vector<vector<int>> subsetsWithDup(vector<int>& nums) {
		vector<vector<int>> ans(1, vector<int>());
		sort(nums.begin(), nums.end());

		int pre_size = 0;
		for (int i = 0; i < nums.size(); i++)
		{
			int n = ans.size();
			for (int j = 0; j < n; j++) //第二个for循环是为了求得包含nums[i]的所有子集
			{
				if (i == 0 || nums[i] != nums[i -1] || j>=pre_size)
				{
					ans.push_back(ans[j]); //在尾部重新插入ans已经存在的子集
					ans.back().push_back(nums[i]); //将nums[i]添加进去
				}
			}
			pre_size = n;
		}
		return ans;
	}
};

时间: 2024-10-14 10:51:19

LeetCode 90:Subsets II的相关文章

leetcode || 90、Subsets II

problem: 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 sol

lintcode 中等题:subsets II 带重复元素的子集

题目 带重复元素的子集 给定一个可能具有重复数字的列表,返回其所有可能的子集 样例 如果 S = [1,2,2],一个可能的答案为: [ [2], [1], [1,2,2], [2,2], [1,2], [] ] 注意 子集中的每个元素都是非降序的 两个子集间的顺序是无关紧要的 解集中不能包含重复子集 挑战 你可以同时用递归与非递归的方式解决么? 解题 一个很简单的想法就是在上一题目中增加判断是否已经存在某个子集 class Solution: """ @param S: A

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

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 题解:Search in Rotated Sorted Array II (旋转已排序数组查找2)

题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 说明: 1)和1比只是有重复的数字,整体仍采用二分查找 2)方法二 : 实现:  

leetcode: Subsets &amp; Subsets II

SubsetsGiven a set of distinct integers, 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,3], a solution is: [ [3], [1], [2], [1,2