[Leetcode] 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,2,3],
[1,3],
[2,3],
[1,2],
[]
]
题意:求数组的所有子集,子集不用如例子中那样排序。
思路:题中要求子集中非降序排列,所以要先进行排序。方法一是使用DFS遍历。如:[1,2,3] ,依次加入[ ]、[1]、[1,2]、[1,2,3]、[1,3]、[2]、[2,3]、[3]。图形化的说明参见这里。代码如下:
class Solution {
public:
vector<vector<int> > subsets(vector<int> &S)
{
vector<vector<int>> res;
vector<int> midArray;
sort(S.begin(),S.end());
getSubsets(S,,midArray,res);
return res;
}
void getSubsets(vector<int> &S,int beg,vector<int> &midArray,vector<vector<int>> &res)
{
res.push_back(midArray);
for(int i=beg;i<S.size();++i)
{
midArray.push_back(S[i]);
getSubsets(S,i+,midArray,res);
midArray.pop_back();
}
}
};
方法二:使用迭代法,思路:拿res中已经存在的元素和新的组合,然后重新放入res中,先给res中放入一个空元素,然后通过空元素和S中第一个元素结合放入res中,以此类推,参考了Grandyang的博客。如:[1,2,3],最开始是空集,那么我们现在要处理1,就在空集上加1,为[1],结果中位[]和[1],下面处理2,在之前的子集基础上,每个都加个2,可以分别得到[2],[1, 2],那么现在所有的子集合为[], [1], [2], [1, 2],同理处理3的情况可得[3], [1, 3], [2, 3], [1, 2, 3], 再加上之前的子集就是所有的子集合了,代码如下:
class Solution {
public:
vector<vector<int> > subsets(vector<int> &S)
{
vector<vector<int>> res(); //放入空
if(S.size()) return res;
sort(S.begin(),S.end());
for(int i=;i<S.size();++i)
{
int midSize=res.size();
for(int j=;j<midSize;++j) //实时结合
{
res.push_back(res[j]);
res.back().push_back(S[i]);
}
}
return res;
}
};
在牛客网上,之前通过,现在显示如下,吐槽一下。

Felix对给定一个集合,求出这个集合所有的子集(所谓子集,就是包含原集合中的一部分元素的集合)进行了总结。
[Leetcode] subsets 求数组所有的子集的更多相关文章
- [Leetcode 90]求含有重复数的子集 Subset II
[题目] Given a collection of integers that might contain duplicates, nums, return all possible subsets ...
- [Leetcode] subsets ii 求数组所有的子集
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
- [Leetcode 78]求子集 Subset
[题目] Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The ...
- [Leetcode 216]求给定和的数集合 Combination Sum III
[题目] Find all possible combinations of k numbers that add up to a number n, given that only numbers ...
- Leetcode之回溯法专题-90. 子集 II(Subsets II)
Leetcode之回溯法专题-90. 子集 II(Subsets II) 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入 ...
- Leetcode之回溯法专题-78. 子集(Subsets)
Leetcode之回溯法专题-78. 子集(Subsets) 给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: nums = ...
- [LeetCode] Find Peak Element 求数组的局部峰值
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- 78 Subsets(求子集Medium)
题目意思:求解一个数组的所有子集,子集内的元素增序排列eg:[1,3,2] result:[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]思路:这是一个递推的过程 [] ...
- LeetCode:Subsets I II
求集合的所有子集问题 LeetCode:Subsets Given a set of distinct integers, S, return all possible subsets. Note: ...
随机推荐
- nodejs--http
http模块主要用到四个方法: 1.Server类 const http = require('http'); let server = new Server(); server.on('reques ...
- Lavavel5.5源代码 - 限流工具
app('redis')->connection('default')->throttle('key000') // 每60秒,只能有10个资源被获取,在3秒内获取不到锁抛出异常 -> ...
- 3.从print到I/O
为何对双引号念念不忘? >>> print("hello, world!") hello, world! 平x而论,既然在意双引号的去掉,为何不在意括号的去掉 ...
- 洛谷P4016 负载平衡问题
题目描述 G 公司有 n 个沿铁路运输线环形排列的仓库,每个仓库存储的货物数量不等.如何用最少搬运量可以使 n 个仓库的库存数量相同.搬运货物时,只能在相邻的仓库之间搬运. 输入输出格式 输入格式: ...
- 给apk签名
一.签名 把apk和签名文件放在jdk bin目录下,然后在jkd bin目录下执行以下代码: jarsigner -verbose -keystore xxx.keystore -signedjar ...
- Linux中java应用程序的部署,使其开机自动启动
初步需求:将在Windows/MyEclipse中开发的java应用程序部署到Linux服务器上,使其运行 针对需求,可以参考下面这些文章,但是这些文章很多东西没有提及到,我自己尝试部署运行 在lin ...
- eclipse 关闭validating
1.起因 validating XXX 总是非常的浪费时间,有时候还会造成程序卡死 2.解决 windows - Perferences - Validation build 全部去掉
- 一丶人生苦短,我用python【第一篇】
1 解释器 解释器(英语:Interpreter),又译为直译器,是一种电脑程序,能够把高级编程语言一行一行直接转译运行.解释器不会一次把整个程序转译出来,只像一位"中间人",每次 ...
- Google无法离线安装扩展程序
Google无法离线安装扩展程序 Chrome插件伴侣 按照里面的使用说明使用 网盘地址: 链接: https://pan.baidu.com/s/1eXoLXyPNl2pfoPnArHq2Lg 提取 ...
- 使用git创建分支
Git大法好--3.Git分支本地操作详解 这时已经切换到了dingBranch分支下面了,在项目文件夹下添加一个dingBranchtest.txt文件,然后提交到本地仓库和远程仓库: git ad ...