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. 深度学习文档 DeepLearning 0.1 documentation

    Contents LICENSE Deep Learning Tutorials Getting Started Download Datasets Notation A Primer on Supe ...

  2. 如何快速把安卓应用移植到BlackBerry 10上

    如何快速把安卓应用移植到BlackBerry 10上 相关博客: BlackBerry相关文档 http://developer.blackberry.com/android/documentatio ...

  3. oc 第五天(内存管理)

    OC的重点: 内存管理 1 基本原理     OC的内存回收机制是和JAVA的自动回收机制是不同的,它有两种模式,或者准确的说是同 一种模式的两种不同体现,下面简单总结下. 1手动内存回收       ...

  4. SQL Server更改排序规则的实现过程

    摘自: http://www.2cto.com/database/201112/115138.html 以下的文章主要向大家描述的是SQL Server更改排序规则的实现过程,以及在实现其实际操作过程 ...

  5. 美团的android多渠道包的3种方法

    转: http://tech.meituan.com/mt-apk-packaging.html 美团Android自动化之旅—生成渠道包 zhihu2014-06-13 10:06 概述 每当发新版 ...

  6. [React] Use the useReducer Hook and Dispatch Actions to Update State (useReducer, useMemo, useEffect)

    As an alternate to useState, you could also use the useReducer hook that provides state and a dispat ...

  7. [RSpec] LEVEL 2 CONFIGURATION & MATCHERS

    Installing RSpec In this level we'll start by getting you setup on a regular Ruby project, then move ...

  8. 读TIJ -7 多形性

    <Think in java·第 7 章  多形性> [面向对象的程序设计语言三种最主要的特征:数据抽象.继承和多态] 在这个层面是没有什么"思想"好谈的!当你依照人们 ...

  9. 微信小程序 - 自定义模态对话框

    更新日期:2018-11-5 微信bug: 在for循环中使用组件时,遮罩层成黑层. 更新时间 2018-9-30 2018-9-30 1.在电脑上调试input超出输入框范围会出现文字模糊以及位移现 ...

  10. OJ帐号保存

    TOJ 614173971 HDU 宇智波佐助 POJ shiai ZOJ henyumen UVa henyumen Light OJ HENYUMEN bzoj henyumen ural 165 ...