(LeetCode 78)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.

题目要求 :
求整数数组的所有子集
注意:
1、子集元素按非降序排列
2、不包含重复的子集
解题思路:
求解这类诸如子集的题目,都可以采用回溯法。(剪枝+递归)
代码如下:
class Solution {
private:
vector<vector<int> > ans;
public:
void collectSubSet(vector<int> &S,vector<int> x,int len,int idx){
if(idx==len){
vector<int> subset;
for(int i=;i<len;i++){
if(x[i]!=)
subset.push_back(S[i]);
}
sort(subset.begin(),subset.end(),less<int>());
ans.push_back(subset);
return;
}
x[idx]=;
collectSubSet(S,x,len,idx+);
x[idx]=;
collectSubSet(S,x,len,idx+);
}
vector<vector<int> > subsets(vector<int> &S) {
int len=S.size();
vector<int> x(len);
// sort(S.begin(),S.end(),greater<int>());
collectSubSet(S,x,len,);
// sort(ans.begin(),ans.end(),cmp());
return ans;
}
};
(LeetCode 78)SubSets的更多相关文章
- LeetCode 78. 子集(Subsets) 34
78. 子集 78. Subsets 题目描述 给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明: 解集不能包含重复的子集. 每日一算法2019/6/6Day 34L ...
- (LeetCode 72)Edit Distance
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- LeetCode(78) Subsets
题目 Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset m ...
- 算法学习笔记(LeetCode OJ)
================================== LeetCode的一些算法题,都是自己做的,欢迎提出改进~~ LeetCode:http://oj.leetcode.com == ...
- 不使用循环或递归判断一个数是否为3的幂(leetcode 326)
326. Power of ThreeGiven an integer, write a function to determine if it is a power of three. Follow ...
- python练习 之 实践出真知 中心扩展法求最大回文子串 (leetcode题目)
1 问题,给定一个字符串,求字符串中包含的最大回文子串,要求O复杂度小于n的平方. 首先需要解决奇数偶数的问题,办法是:插入’#‘,aba变成#a#b#a#,变成奇数个,aa变成#a#a#,变成奇数个 ...
- (LeetCode 41)First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- (LeetCode 153)Find Minimum in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- (LeetCode 160)Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
随机推荐
- SB!SB!SB! ----WriteUp
原题 下载图片 http://ctf5.shiyanbar.com/stega/ste.png 用Stegsolve查看 发现有个二维码 扫码可以知道flag
- bzoj 3283 扩展BSGS + 快速阶乘
T2 扩展BSGS T3 快速阶乘 给定整数n,质数p和正整数c,求整数s和b,满足n! / pb = s mod pc 考虑每次取出floor(n/p)个p因子,然后将问题转化为子问题. /*** ...
- Codeforces Round #354 (Div. 2) B. Pyramid of Glasses 模拟
B. Pyramid of Glasses 题目连接: http://www.codeforces.com/contest/676/problem/B Description Mary has jus ...
- UVALive 6262 Darts
Description Consider a game in which darts are thrown at a board. The board is formed by 10 circles ...
- Python—对Excel进行读写操作
学习Python的过程中,我们会遇到Excel的读写问题.通过搜索得知,我们可以使用xlwt module将数据写入Excel表格,使用xlrd module从Excel读取数据.下面介绍如何实现使用 ...
- php安装配置
Content 0.序 1.安装前准备 2.安装PHP 3.配置php-fpm 0.序 本文主要是记录php在 Centos下的安装配置 .文中如无特别说明.表示php-5.6.31代码目录. 1.安 ...
- .Net 中DataTable和 DataRow的 区别与联系
1.简要说明二者关系 DataRow 和 DataColumn 对象是 DataTable 的主要组件.使用 DataRow 对象及其属性和方法检索.评估.插入.删除和更新 DataTable 中的值 ...
- Run native executable in Android App
Run native executable in Android App Demo † Here's demo application called "Run Native Exe" ...
- win7 系统盘怎样瘦身! 可整理出4-5G。
1.移走虚拟内存文件到非系统盘 大家都知道,为了加快系统的执行,Windows提供了虚拟内存机制,而在Windows7中,默认是开启这项功能的,并且虚拟内存文件在系统盘.比方一台2G内存的机器,虚拟内 ...
- winform datagridview 打印
转载:http://www.cnblogs.com/Irving/archive/2012/10/12/2721666.html c#实现打印功能 http://www.cnblogs.com/zhc ...