Subsets 子集系列问题 leetcode

子集系列问题

Coding 问题中有时会出现这样的问题:给定一个集合,求出这个集合所有的子集(所谓子集,就是包含原集合中的一部分元素的集合)。

或者求出满足一定要求的子集,比如子集中元素总和为定值,子集元素个数为定值等等。

我把它们归类为子集系列问题。

leetcode上原题链接

思路分析:

思路一

可以用递推的思想,观察S=[], S =[1], S = [1, 2] 时解的变化。

可以发现S=[1, 2] 的解就是 把S = [1]的所有解末尾添上2,然后再并上S = [1]里面的原有解。因此可以定义vector<vector<int> > 作为返回结果res, 开始时res里什么都没有,第一步放入一个空的vecotr<int>,然后这样迭代n次,每次更新res 内容,最后返回res。

 1 class Solution {
 2 public:
 3     vector<vector<int>> subsets(vector<int>& nums)
 4     {
 5         vector<vector<int>> res;
 6         vector<int> temp;
 7         res.push_back(temp);
 8         if(nums.empty())
 9             return res;
10         sort(nums.begin(),nums.end());
11         for(vector<int>::iterator i=nums.begin();i!=nums.end();i++)
12         {
13            int size=res.size();
14            for(int j=0;j<size;j++)//这里注意因为res一直在增长,所以遍历res的时候不能用vector<int>::iterator,否则可能因为vector重新allocate内存而地址失效,因此直接使用数组下标。
15            {
16                 vector<int> tem;
17                 for(auto begin=res[j].begin();begin!=res[j].end();begin++)//仅仅是为了完成在末尾添加新元素(复制+末尾插入)
18                     tem.push_back(*begin);
19                 tem.push_back(*i);
20                 res.push_back(tem);
21            }
22
23         }
24         return res;
25     }
26 };
时间: 2024-06-22 21:14:25

Subsets 子集系列问题 leetcode的相关文章

Leetcode:Subsets 子集生成

Subsets: Given 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

子集系列(二) 满足特定要求的子集,例 [LeetCode] Combination, Combination Sum I, II

引言 既上一篇 子集系列(一) 后,这里我们接着讨论带有附加条件的子集求解方法. 这类题目也是求子集,只不过不是返回所有的自己,而往往是要求返回满足一定要求的子集. 解这种类型的题目,其思路可以在上一篇文章的思路略作改进. 例 1,求元素数量为定值的所有子集 Combinations Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For example,If n 

leetCode 78.Subsets (子集) 解题思路和方法

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

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 Subsets I&amp; II——递归

I Given 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,3],

LeetCode(90):子集 II

Medium! 题目描述: 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: [ [2], [1], [1,2,2], [2,2], [1,2], [] ] 解题思路: 这道子集合之二是之前那道Subsets 子集合 的延伸,这次输入数组允许有重复项,其他条件都不变,只需要在之前那道题解法的基础上稍加改动便可以做出来,我们先来看非递归解法,拿题目中的例子[1 2 2]来分析,根据之前Subse

从n个元素中选择k个的所有组合(包含重复元素)

LeetCode:Combinations这篇博客中给出了不包含重复元素求组合的5种解法.我们在这些解法的基础上修改以支持包含重复元素的情况.对于这种情况,首先肯定要对数组排序,以下不再强调 修改算法1:按照求包含重复元素集合子集的方法LeetCode:Subsets II算法1的解释,我们知道:若当前处理的元素如果在前面出现过m次,那么只有当前组合中包含m个该元素时,才把当前元素加入组合 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

careercup-递归和动态规划 9.4

9.4 编写一个方法,返回某集合的所有子集. 类似leetcode:Subsets 解法: 解决这个问题之前,我们先要对时间和空间复杂度有个合理的评估.一个集合会有多少子集?我们可以这么计算,生成了一个子集时,每个元素都可以“选择”在或者不在这个子集中.也就是说,第一个元素有两个选择:它要么在集合中,要么不在集合中.同样,第二个元素也有两个选择,以此类推,2相乘n次等于2^n个子集.因此,在时间和空间复杂度上,我们不可能做得比O(2^n)更好. 解法一:递归 首先将空集合加入,则当前集合为{{}

01背包//简直要被这道题玩死(掀桌)

先上链接: 表格什么的最清楚了:http://blog.csdn.net/mu399/article/details/7722810 dd大大的背包九讲: —————————————————— 01背包的状态转换方程 f[i,j] = Max{ f[i-1,j-Wi]+Pi( j >= Wi ),  f[i-1,j] } f[i,j]表示在前i件物品中选择若干件放在承重为 j 的背包中,可以取得的最大价值. Pi表示第i件物品的价值. 决策:为了背包中物品总价值最大化,第 i件物品应该放入背包中