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],
[]
]

Summary: Recursive approach, we can optimize it.

     vector<vector<int> > subsets(vector<int> &S) {
std::sort(S.begin(), S.end());
vector<vector<int> > result;
vector<int> subset;
result.push_back(subset);
if(S.size() == )
return result;
for(int i = ; i < S.size(); i ++) {
vector<int> sub_s(S.begin() + i + , S.end());
vector<vector<int> > ret = subsets(sub_s);
for(auto item : ret){
item.insert(item.begin(), S[i]);
result.push_back(item);
}
} return result;
}

Subsets [LeetCode]的更多相关文章

  1. Subsets —— LeetCode

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

  2. Subsets LeetCode总结

    Subsets 题目 Given a set of distinct integers, nums, return all possible subsets. Note: The solution s ...

  3. [LeetCode] Backtracking Template for (Subsets, Permutations, and Combination Sum)

    根据issac3 用Java总结了backtracking template, 我用他的方法改成了Python. 以下为template. def backtrack(ans, temp, nums, ...

  4. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  5. Solution to LeetCode Problem Set

    Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...

  6. [LeetCode] Subsets II 子集合之二

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...

  7. [LeetCode] Subsets 子集合

    Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...

  8. LeetCode:Subsets I II

    求集合的所有子集问题 LeetCode:Subsets Given a set of distinct integers, S, return all possible subsets. Note: ...

  9. 【leetcode】Subsets (Medium) ☆

    Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...

随机推荐

  1. String一点小发现

    今天面试官问了几个关于java内存方面的问题,其中有一个是关于内存重复使用的.突然想到java中String比较特殊的地方,根据自己的理解所以稍微记录一下以免遗忘. 对于下面这个小程序: public ...

  2. Java——再看IO

    一.编码问题 utf-8编码中,一个中文占3个字节,一个英文占1个字节:gbk编码中,一个中文占2个字节,一个英文占1个字节. Java是双字节编码,为utf-16be编码,是说一个字符(无论中文还是 ...

  3. JAVA排序--[快速排序]

    package com.array; public class Sort_Quick { /* * 项目名称:快速排序 ; * 项目要求:用JAVA对数组进行排序,并运用快速排序算法; * 作者:Se ...

  4. data-*属性——使用自定义属性的方式存储数据

    HTML5提供了data-*属性能存储页面或应用程序的私有自定义数据.只需在属性前加上data-前缀即可,值可以是任意字符串. 存储的(自定义)数据能够被页面的 JavaScript 中利用,以创建更 ...

  5. 异步上传图片,光用jquery不行,得用jquery.form.js插件

    异步上传图片,光用jquery不行,得用jquery.form.js插件,百度一下下载这个插件,加jquery,引入就可以了 <form id="postbackground" ...

  6. mouseover和mouseenter的区别

    mouseover和mouseenter都是鼠标事件. mouseover事件,当鼠标穿过被选元素的时候,若此元素有子元素,子元素也会被触发此事件.即是使用mouseover事件,会多次触发此元素. ...

  7. Java的内存分配策略

    简单来说,对象内存分配主要是在堆中分配.但是分配的规则并不是固定的,取决于使用的收集器组合以及JVM内存相关参数的设定 以下介绍几条基本规则(使用的ParNew+Serial Old收集器组合): 一 ...

  8. Maven测试

    生命周期阶段需要绑定到某个插件的目标才能完成真正的工作,test阶段正是与maven-surefire-plugin的test目标相绑定了,这是一个 内置的绑定. 在默认情况下,maven-suref ...

  9. select实现输入模糊匹配与选择双重功能

    下载jqueryUI插件 引入 <link rel="stylesheet" type="text/css" href="/js/jquery/ ...

  10. mysql 大数据量的处理

    insert 1.过滤一段时间内重复的数据2.数据缓存起来,批量写入 select1.使用分区表2.主主复制,连接不同的mysql3.建立索引4.定时求平均值,写入一个新的表中