Given a set of distinct integers, return all possible subsets.

Notice
  • Elements in a subset must be in non-descending order.
  • The solution set must not contain duplicate subsets.
Example

If S = [1,2,3], a solution is:

[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
Challenge

Can you do it in both recursively and iteratively?

题意

给定一个含不同整数的集合,返回其所有的子集

注意事项

子集中的元素排列必须是非降序的,解集必须不包含重复的子集

解法一:

 class Solution {
public:
/*
* @param nums: A set of numbers
* @return: A list of lists
*/
vector<vector<int>> subsets(vector<int> &nums) {
// write your code here
vector<vector<int> > results;
vector<int> result; sort(nums.begin(), nums.end()); helper(nums, , result, results); return results;
} void helper(vector<int> &nums, int start, vector<int> & result, vector<vector<int> > & results)
{
results.push_back(result); for (int i = start; i < nums.size(); ++i) {
result.push_back(nums[i]); helper(nums, i + , result, results); result.pop_back();
}
}
};

17. Subsets【medium】的更多相关文章

  1. CF895C: Square Subsets && 【BZOJ2844】albus就是要第一个出场

    CF895C: Square Subsets && [BZOJ2844]albus就是要第一个出场 这两道题很类似,都是线性基的计数问题,解题的核心思想也一样. CF895C Squa ...

  2. 2. Add Two Numbers【medium】

    2. Add Two Numbers[medium] You are given two non-empty linked lists representing two non-negative in ...

  3. 92. Reverse Linked List II【Medium】

    92. Reverse Linked List II[Medium] Reverse a linked list from position m to n. Do it in-place and in ...

  4. 82. Remove Duplicates from Sorted List II【Medium】

    82. Remove Duplicates from Sorted List II[Medium] Given a sorted linked list, delete all nodes that ...

  5. 61. Search for a Range【medium】

    61. Search for a Range[medium] Given a sorted array of n integers, find the starting and ending posi ...

  6. 62. Search in Rotated Sorted Array【medium】

    62. Search in Rotated Sorted Array[medium] Suppose a sorted array is rotated at some pivot unknown t ...

  7. 74. First Bad Version 【medium】

    74. First Bad Version [medium] The code base version is an integer start from 1 to n. One day, someo ...

  8. 75. Find Peak Element 【medium】

    75. Find Peak Element [medium] There is an integer array which has the following features: The numbe ...

  9. 159. Find Minimum in Rotated Sorted Array 【medium】

    159. Find Minimum in Rotated Sorted Array [medium] Suppose a sorted array is rotated at some pivot u ...

随机推荐

  1. linux服务器之间的文件同步;rsync+inotifywait;同步多个目录

    1.双向同步:unison+inotify 2.单向同步:rsync+inotify python版的pyinotify 本文介绍第二种方法: 1.Inotify 是一个 Linux特性,它监控文件系 ...

  2. Python爬虫(3)豆瓣登录

    前面(1)(2)的内容已经足够爬虫如链家网之类的不需要登录可以直接获取数据的网站. 而要爬取社交网站比较鲜明的特点就是需要登录,否则很多东西都无法获取.经过测试发现,微博,知乎都不是很好登录,知乎有时 ...

  3. JavaScript实现计算器功能

    截图 : cal.js var Class = {} ; Class.calculation = function(){ var calculation = {} ; calculation.resu ...

  4. 用 Git Hooks 进行自动部署

    原文发表于 http://ourai.ws/posts/deployment-with-git-hooks/ 昨天开始接手开发公司前端团队的主页,在稍微修改点东西后推送到远程仓库想看下线上结果时发现并 ...

  5. luigi操作hive表

    关于luigi框架下查询hive表的操作 class JoinQuery(HiveQueryTask): date=luigi.DateParameter() def hiveconfs(self): ...

  6. php 上传视频的代码,

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  7. 如何解决Ubuntu与Windows双系统时间不同步

    导读 不知道有没朋友跟我一样是 Ubuntu 和 Windows 双系统?今天有朋友问到我,当他从 Ubuntu 系统重新启动到 Windows 时,会发现 Windows 中的时间变了,他问我有没办 ...

  8. 如何:创建签名的友元程序集(C# 和 Visual Basic)

    如何:创建签名的友元程序集(C# 和 Visual Basic) Visual Studio 2013    本示例演示了如何将友元程序集和具有强名称的程序集一起使用. 这两种程序集必须都使用强名称. ...

  9. ant design pro (九)引入外部模块

    一.概述 原文地址:https://pro.ant.design/docs/import-cn 除了 antd 组件以及脚手架内置的业务组件,有时我们还需要引入其他外部模块,这里以引入富文本组件 re ...

  10. model中设置默认值时 ,使用 lambda 与否的差别以及datetime的默认值方法

    'date': '2013-01-01'  #固定值 'date': time.strftime('%Y-%m-%d')  #启动时候的值 'date': lambda *a: time.strfti ...