[leetcode]#78 Subsets
2015-10-21 11:12
344 查看
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,3],
[1,3],
[2,3],
[1,2],
[]
]
列出一个集合的所有子集
解题思路:1、看到这种生成多组数据的,而且富有规律性的,想到用递归生成。
2、将原集合的元素标号为0,1,2…n
3、递归调用后,每次将生成的集合压入容器a,并判断当前最大标号currentIndex是否为n,不为n,就继续循环添加元素currentIndex+1…n。
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,3],
[1,3],
[2,3],
[1,2],
[]
]
列出一个集合的所有子集
解题思路:1、看到这种生成多组数据的,而且富有规律性的,想到用递归生成。
2、将原集合的元素标号为0,1,2…n
3、递归调用后,每次将生成的集合压入容器a,并判断当前最大标号currentIndex是否为n,不为n,就继续循环添加元素currentIndex+1…n。
[code]#include <algorithmn> class Solution { public: void subset( vector<vector<int>>& a, vector<int>& nums, vector<int> b, int currentIndex) { sort(b.begin(), b.end()); a.push_back(b); if (currentIndex+1 == nums.size()) return; for (int i = currentIndex+1; i < nums.size(); i++) { vector<int> c = b; c.push_back(nums[i]); subset(a, nums, c, i); } } vector<vector<int>> subsets(vector<int>& nums) { vector<vector<int>> a; vector<int> b; subset(a, nums, b, -1); return a; } };
相关文章推荐
- [MFC]CTreeView树形视图、CTreeCtrl树形控件
- jquery部分用法
- 链表基本操作
- BI ( 商务智能 ) 简介
- Ubuntu下HBase安装过程
- n人站圈报数,3 出列。 最后留下的人原来的位置 3种方式
- 网上数据格式化数据
- 优秀程序员的 18 大法则
- LNMP环境编译安装全过程(php-5.6.14、nginx-1.8.0、mysql-5.6.26)
- Python学习笔记2
- 转: SVN和Git的一些用法总结
- postgresql远程连接备份
- java学习笔记
- Android TouchEvent事件传递机制
- Redis 键空间通知
- mysql 备份与恢复
- 防止表单重复提交
- loadView、viewDidLoad及viewDidUnload的关系
- IOS 代码块block :capturing self strongly in this block is 告警
- 视频框架 Vitamio 使用教程+部分心得(一)准备