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. CentOS 7一些常用配置

    一.更改启动模式 背景:个人开发环境,安装了GNOME桌面,默认启动是图形化模式. 修改为命令行模式. systemctl set-default multi-user.target 二.命令行模式下 ...

  2. 将NuGet配置到环境变量中

    https://docs.nuget.org/consume/command-line-reference Installing The NuGet command line may be insta ...

  3. js数组去重的三种常用方法总结

    第一种是比较常规的方法 思路: 1.构建一个新的数组存放结果 2.for循环中每次从原数组中取出一个元素,用这个元素循环与结果数组对比 3.若结果数组中没有该元素,则存到结果数组中   Array.p ...

  4. Redis事务的分析及改进

    Redis事务的分析及改进 Redis的事务特性 数据ACID特性满足了几条? 为了保持简单,redis事务保证了其中的一致性和隔离性: 不满足原子性和持久性: 原子性 redis事务在执行的中途遇到 ...

  5. implement "slam_karto" package on a Freight robot

    1. login ssh fetch@<robot ip or robot name> 2.  set robot master modify .bashrc in robot's com ...

  6. “System.Data.OracleClient.OracleConnection”已过时

    处理办法: 在oracle 安装目录下 找到 Oracle.DataAccess.dll添加引用,然后 using Oracle.DataAccess.Client;其他的都不用动,即可.连接字符串中 ...

  7. HTML笔记(七)head相关元素<base> & <meta>

    <head>元素是所有头部元素的容器. 可添加的标签有:<title>.<base>.<link>.<meta>.<script> ...

  8. iOS - OC 异常处理

    1.@try 语句 @try { // Code that can potentially throw an exception 可能会抛出异常的代码块 statement . . . } @catc ...

  9. 实现jQuery扩展总结

    开发自己需要的jQuery插件,看个示例说明<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"&qu ...

  10. BigTale

    Google's BigTable 原理 (翻译)     题记:google 的成功除了一个个出色的创意外,还因为有 Jeff Dean 这样的软件架构天才.                     ...