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排序o(nlogn);

假设S为abcd...,然后遍历S中每个数,

对a,返回集合[ [ ],  [a] ]

对b,返回集合{ [ ],  [a], [b], [a, b] }

由此看出,每出现一个新值,就是保存返回集合中原有数组不变,并在每个原有数组后加上新值,组合成新数组,添加到返回集合的后面;

代码:

 class Solution {
public:
vector<vector<int> > subsets(vector<int> &S) {
sort(S.begin(), S.end());
vector<vector<int> > ret;
ret.push_back(vector<int> ()); for (int i = ; i < S.size(); ++i) {
int pre_size = ret.size();
for (int j = ; j < pre_size; ++j) {
vector<int> new_item(ret[j]);
new_item.push_back(S[i]);
ret.push_back(new_item);
}
}
return ret;
}
};

附录:

C++ vector 浅复制、深复制

【Leetcode】【Medium】Subsets的更多相关文章

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  4. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  5. 【leetcode刷题笔记】Subsets II

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

  6. 【leetcode刷题笔记】Subsets

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

  7. 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists

    [Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...

  8. 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman

    [Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...

  9. 【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number

    [Q7]  把数倒过来 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outpu ...

  10. 【LeetCode算法题库】Day1:TwoSums & Add Two Numbers & Longest Substring Without Repeating Characters

    [Q1] Given an array of integers, return indices of the two numbers such that they add up to a specif ...

随机推荐

  1. npm全局安装和局部文件安装区别

    全局安装往往是安装一个工具,他不是安装在一个文件夹下,而是安装在某个全局环境下,如目前我的安装路径是: C:\Users\cvter\AppData\Roaming\npm 在这里,我们可以看到所有全 ...

  2. Jmeter调试脚本之断言

    前言: jmeter中有个元件叫做断言(Assertion),它的作用和loadrunner中的检查点类似: 用于检查测试中得到的响应数据等是否符合预期,用以保证性能测试过程中的数据交互与预期一致. ...

  3. 游戏中的网络同步机制——Lockstep(帧同步)

    本文来自: https://bindog.github.io/blog/2015/03/10/synchronization-in-multiplayer-networked-game-lockste ...

  4. Learning Linux Commands: awk--reference

    http://how-to.linuxcareer.com/learning-linux-commands-awk 1. Introduction In this case, the title mi ...

  5. 有向图和无向图的数组C++实现

    源码:https://github.com/cjy513203427/C_Program_Base/tree/master/55.%E5%9B%BE 结点类Noded.h 不需要存储索引 #pragm ...

  6. Splunk数据处理

    0.提要 本篇主要从技术层面针对Splunk Enterprise中关于数据处理的概念.过程与部件进行了概要性总结. 1.数据管理基本概念 索引(index):Splunk用于存储事件的数据仓库: 索 ...

  7. MySQL:ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061)

    错误 原因:可能是服务没有启动 以管理员身份打开cmd 输入 net start mysql

  8. shodan在渗透测试中的应用

    场景1:想搜索美国所有的elasticsearch服务器 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.设计 ...

  9. IDEA创建Maven项目和子模块

    一.新建Project a. 新建项目,点击Create New Project b. 选择使用maven来新建项目 选择新建Maven模块(对IDEA来说是项目) 如果Project SDK那里没有 ...

  10. 建一个springboot项目

    1.打开 https://start.spring.io/ 新建一个maven的demo,这里选择的是1.5.18的版本 2.将自动生成的demo导入eclipse,直接选中File里的import, ...