(LeetCode 78)SubSets
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.

题目要求 :
求整数数组的所有子集
注意:
1、子集元素按非降序排列
2、不包含重复的子集
解题思路:
求解这类诸如子集的题目,都可以采用回溯法。(剪枝+递归)
代码如下:
class Solution {
private:
vector<vector<int> > ans;
public:
void collectSubSet(vector<int> &S,vector<int> x,int len,int idx){
if(idx==len){
vector<int> subset;
for(int i=;i<len;i++){
if(x[i]!=)
subset.push_back(S[i]);
}
sort(subset.begin(),subset.end(),less<int>());
ans.push_back(subset);
return;
}
x[idx]=;
collectSubSet(S,x,len,idx+);
x[idx]=;
collectSubSet(S,x,len,idx+);
}
vector<vector<int> > subsets(vector<int> &S) {
int len=S.size();
vector<int> x(len);
// sort(S.begin(),S.end(),greater<int>());
collectSubSet(S,x,len,);
// sort(ans.begin(),ans.end(),cmp());
return ans;
}
};
(LeetCode 78)SubSets的更多相关文章
- LeetCode 78. 子集(Subsets) 34
78. 子集 78. Subsets 题目描述 给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明: 解集不能包含重复的子集. 每日一算法2019/6/6Day 34L ...
- (LeetCode 72)Edit Distance
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- LeetCode(78) Subsets
题目 Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset m ...
- 算法学习笔记(LeetCode OJ)
================================== LeetCode的一些算法题,都是自己做的,欢迎提出改进~~ LeetCode:http://oj.leetcode.com == ...
- 不使用循环或递归判断一个数是否为3的幂(leetcode 326)
326. Power of ThreeGiven an integer, write a function to determine if it is a power of three. Follow ...
- python练习 之 实践出真知 中心扩展法求最大回文子串 (leetcode题目)
1 问题,给定一个字符串,求字符串中包含的最大回文子串,要求O复杂度小于n的平方. 首先需要解决奇数偶数的问题,办法是:插入’#‘,aba变成#a#b#a#,变成奇数个,aa变成#a#a#,变成奇数个 ...
- (LeetCode 41)First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- (LeetCode 153)Find Minimum in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- (LeetCode 160)Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
随机推荐
- bzoj 1337 最小圆覆盖
/************************************************************** Problem: 1337 User: idy002 Language: ...
- .net正则提取手机号码,并替换带有手机号码的a标签
//用正则查找字符串中的手机号码,最后替换成带a标签的可打电话的字符串 var str = "收件人:江苏省苏州市工业园区屌丝大叔收,电话:18688888888"; var re ...
- Vanish 详解
1.varnish 概述: varnish是一款高性能且开源的方向代理服务器和HTTP加速器,它的开发者poul-Henning kamp FreeBSD 核心的开发人员之一.varnish采 ...
- Git_创建标签
在Git中打标签非常简单,首先,切换到需要打标签的分支上: $ git branch * dev master $ git checkout master Switched to branch 'ma ...
- 伟大的通信使者——JSON(JavaScript版本)
JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式.它基于 ECMAScript 规范的一个子集,采用完全独立于编程语言的文本格式来存储和表 ...
- 华为S5300系列交换机V100R005SPH021升级补丁
S23_33_53-V100R005SPH021.pat 附件: 链接:https://pan.baidu.com/s/1xaEZa8hn8psHSQXrk2d9yA 密码:9b6o
- MySql篇
CentOS6下通过yum安装的MySQL是5.1版的,比较老,所以就想通过源代码安装高版本的5.6.26. 一:卸载旧版本 使用下面的命令检查是否安装有MySQL Server rpm -qa | ...
- Windows Azure 系列-- Azure Queue的操作
- Storage Account. 和之前介绍的Azure Table和AzureBlob一样.你须要一个StorageAccount,仅仅须要创建1次AzureStorageAccount就好了, ...
- ISO 7816-4: Interindustry Commands for Interchange
5. Basic Organizations 5.1 Data structures5.2 Security architecture of the card 5.3 APDU message str ...
- Android 通话记录分析
http://stackoverflow.com/questions/6786666/how-do-i-access-call-log-for-android http://android2011de ...