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的更多相关文章

  1. LeetCode 78. 子集(Subsets) 34

    78. 子集 78. Subsets 题目描述 给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明: 解集不能包含重复的子集. 每日一算法2019/6/6Day 34L ...

  2. (LeetCode 72)Edit Distance

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  3. LeetCode(78) Subsets

    题目 Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset m ...

  4. 算法学习笔记(LeetCode OJ)

    ================================== LeetCode的一些算法题,都是自己做的,欢迎提出改进~~ LeetCode:http://oj.leetcode.com == ...

  5. 不使用循环或递归判断一个数是否为3的幂(leetcode 326)

    326. Power of ThreeGiven an integer, write a function to determine if it is a power of three. Follow ...

  6. python练习 之 实践出真知 中心扩展法求最大回文子串 (leetcode题目)

    1 问题,给定一个字符串,求字符串中包含的最大回文子串,要求O复杂度小于n的平方. 首先需要解决奇数偶数的问题,办法是:插入’#‘,aba变成#a#b#a#,变成奇数个,aa变成#a#a#,变成奇数个 ...

  7. (LeetCode 41)First Missing Positive

    Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...

  8. (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 ...

  9. (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 ...

随机推荐

  1. 聊聊用CSS3来玩立方体

    声明:本文为原创文章,如需转载,请注明来源WAxes,谢谢! 虽然现在很多浏览器都还不支持css3的3D转换,不过估计也已经有很多人都有玩css3的3D了......所以我这篇也就相当于水一下了,哈哈 ...

  2. Codeforces Round #359 (Div. 2) B. Little Robber Girl's Zoo 水题

    B. Little Robber Girl's Zoo 题目连接: http://www.codeforces.com/contest/686/problem/B Description Little ...

  3. Codeforces Round #295 (Div. 2)A - Pangram 水题

    A. Pangram time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...

  4. 《学习OpenCv》 笔记(1)

    P1-P17 废话 可跳过 不过讲了如何搭建环境,如果你没有搭建的话,可以查看我的另外一个博文,详细讲了如何构建OpenCv的编程环境 P19 开始编写第一个代码

  5. PUSH MESSAGE 云控等交互类测试业务的自动化

    针对的业务: 1. PUSH消息,即由云端服务向客户端推送消息 2. MESSAG消息,即用户间消息.用户群消息和聊天室消息 上干货,框架见下图:

  6. spring mvc接收数组

    (一)前言 对于springmvc接收数组的问题啊,我试验过几次,但是了有时候成功了,有时候失败了,也不知道为啥的,然后现在又要用到了,所以打算具体看看到底怎么回事,但是了我实验成功了顺便找了好多资料 ...

  7. java基础学习总结——对象转型

    一.对象转型介绍 对象转型分为两种:一种叫向上转型(父类对象的引用或者叫基类对象的引用指向子类对象,这就是向上转型),另一种叫向下转型.转型的意思是:如把float类型转成int类型,把double类 ...

  8. mysql 连接出错 'mysqladmin flush-hosts'

    本文章 转载于: http://blog.itechol.com/space-33-do-blog-id-5670.html    求助QQ:499628121   环境说明:   内网测试服务器19 ...

  9. arcgis连接excel出现数据库失败 外部数据库驱动程序意外错误

    微软搞事情,删除以下更新就行:win7 KB4041678 KB4041681SERVER 2008 R2 KB4041678 KB4041681WIN10 KB4041676 KB4041691SE ...

  10. .NET:“事务、并发、并发问题、事务隔离级别、锁”小议,重点介绍:“事务隔离级别"如何影响 “锁”?

    备注 我们知道事务的重要性,我们同样知道系统会出现并发,而且,一直在准求高并发,但是多数新手(包括我自己)经常忽略并发问题(更新丢失.脏读.不可重复读.幻读),如何应对并发问题呢?和线程并发控制一样, ...