Leetcode_subsets
来源:互联网 发布:航盛导航端口 编辑:程序博客网 时间:2024/06/10 01:19
地址:http://oj.leetcode.com/problems/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], []]思路:用递归来解决,S的size为n,则一共有2的n次方的子集合,之所以是这样,就是因为对于S中的每一个元素,都可以出现或者不出现在某个集合中。
参考代码:
vector<vector<int> >res;void func(vector<int>&vec, vector<int>subres){ if(vec.empty()) { sort(subres.begin(), subres.end()); res.push_back(subres); return; } vector<int>subvec(vec.begin(), vec.end()-1); subres.push_back(vec.back()); func(subvec, subres); subres.pop_back(); func(subvec, subres);}class Solution {public: vector<vector<int> > subsets(vector<int> &S) { res.clear(); if(!S.empty()) { vector<int>subres; func(S, subres); } return res; }};
0 0
- Leetcode_subsets
- LeetCode_Subsets
- LeetCode_Subsets II
- Leetcode_subsets-ii
- LeetCode_Subsets II
- 服务器编程入门(10)TCP回射服务器实现 - 并发
- reportviewer 解决按钮tips英文问题
- 游戏公司日记(5)
- Makefile 的变量-2
- Python正则表达式的用法
- Leetcode_subsets
- 使自己的应用出现在分享列表
- mysql常用命令
- Chapter 1 Hello, world of concurrency in C++!
- svn: 方法 OPTIONS 失败于 “https://192.168.1.110/svn/amlogic_mx/software/turnkey_src”: SSL handshake fail
- MyBatis_与spring的整合之添加事务
- struts2 拦截器 登录权限验证
- 03 Linux中C语言头文件位置
- 吞吐量(Throughput)、QPS、并发数、响应时间(RT)对系统性能的影响