(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 3483 SGU505 Prefixes and suffixes(字典树+可持久化线段树)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3483 [题目大意] 给出一些串,同时给出m对前缀后缀,询问有多少串满足给出的前缀后缀模 ...
- Linux怎么开启ssh
一.查看ssh开启状态 service ssh status 这是已经开启了的状态 二.如果没有开启 键入以下命令开启 service ssh start 三.开启后如果不能利用xshell远程访问 ...
- 王彪-20162321-Java程序设计与数据结构2nd-第十周学习总结
学习目标 讨论有向图和无向图 定义带权图并讨论它们的应用 定义图的广度优先遍历和深度优先遍历 定义最小生成树 讨论图的实现策略 书中图的基本定义 图是由结点及结点间的连接组成的,结点称为顶点,结点间的 ...
- 【最短路Dijistra】【一般堆优化】【配对堆优化】
突然觉得堆优化$O(log_n)$的复杂度很优啊,然而第n次忘记了$Dijistra$怎么写QAQ发现之前都是用的手写堆,这次用一下$stl$ #include<bits/stdc++.h> ...
- bzoj 3211 线段树
开方操作最多进行5次就可以把出现的任何数变成1. 所以用线段树暴力修改,以后修改时只需看一下是否当前区间都是0或1,如果是那么就直接返回. /***************************** ...
- bzoj1477 poj1061 青蛙的约会
Description 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要的事 ...
- HDU 5694 BD String 迭代
BD String 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5694 Description Problem Description 众所周知, ...
- Codeforces Round #350 (Div. 2) C. Cinema 水题
C. Cinema 题目连接: http://www.codeforces.com/contest/670/problem/C Description Moscow is hosting a majo ...
- opencv第二课 进行缩放图片~
#include<stdio.h> #include<iostream> #include<opencv2\opencv.hpp> using namespace ...
- Clever Little Box 电缆组件 USB A 插头 至 USB B 插头
http://china.rs-online.com/web/p/usb-cable-assemblies/7244143/ 产品详细信息 USB3.0适配器 superspeed USB将提供10x ...