[Leetcode] subsets ii 求数组所有的子集
Given a collection of integers that might contain duplicates, 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,2], a solution is:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]
题意:可能有重复的元素,返回所有子集。
思路:这题是subsets的扩展。大致的思路也是相同的,先是对给定整数进行排序,这样就可以保证所求的子集都是非降序的;在循环或递归的过程中跳过重复的。DFS法的代码如下:
class Solution {
public:
vector<vector<int> > subsets(vector<int> &S)
{
vector<vector<int>> res;
vector<int> midArray;
sort(S.begin(),S.end());
getSubsets(S,,midArray,res);
return res;
}
void getSubsets(vector<int> &S,int beg,vector<int> &midArray,vector<vector<int>> &res)
{
res.push_back(midArray);
for(int i=beg;i<S.size();++i)
{
midArray.push_back(S[i]);
getSubsets(S,i+,midArray,res);
midArray.pop_back();
while(S[i]==S[i+]) //跳过重复的元素
i++;
}
}
};
迭代法:代码来源Grandyang的博客。其主要思路是,定义变量last记录下有重复的开始,因为第一个重复的元素,已经和之前res中的区间形成新的一部分,第二或其以后的重复元素,只要和新形成的部分结合就好,因为之前的已经结合过了。代码如下:
class Solution {
public:
vector<vector<int> > subsets(vector<int> &S)
{
vector<vector<int>> res();
if(S.size()==) return res;
sort(S.begin(),S.end());
int size=res.size();
if(S.size()==) return res;
int last=S[];
for(int i=;i<S.size();++i)
{
if(last !=S[i])
{
last=S[i];
size=res.size();
}
int newSize=res.size();
for(int j=newSize-size;j<newSize;++j)
{
res.push_back(res[j]);
res.back().push_back(S[i]);
}
}
return res;
}
};
[Leetcode] subsets ii 求数组所有的子集的更多相关文章
- [Leetcode] subsets 求数组所有的子集
Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...
- LeetCode Subsets II (DFS)
题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...
- [LeetCode] Subsets II 子集合之二
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
- [leetcode]Subsets II @ Python
原题地址:https://oj.leetcode.com/problems/subsets-ii/ 题意: Given a collection of integers that might cont ...
- [LeetCode] H-Index II 求H指数之二
Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize ...
- lintcode 中等题:subsets II 带重复元素的子集
题目 带重复元素的子集 给定一个可能具有重复数字的列表,返回其所有可能的子集 样例 如果 S = [1,2,2],一个可能的答案为: [ [2], [1], [1,2,2], [2,2], [1,2] ...
- 【leetcode题目整理】数组中找子集
368. Largest Divisible Subset 题意:找到所有元素都不同的数组中满足以下规则的最大子集,规则为:子集中的任意两个元素a和b,满足a%b=0或者b%a=0. 解答:利用动态规 ...
- [Leetcode] Subsets II
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
- [LeetCode] Subsets II [32]
题目 Given a collection of integers that might contain duplicates, S, return all possible subsets. Not ...
随机推荐
- 用bootstrap框架弄的网站。(首页)
网站的每一处代码都加上注解,以便浏览! 效果图: <!doctype html> <html lang="zh-cn"> <head> ...
- python3 练习题100例 (二十六)回文数判断
题目内容: 给一个5位数,判断它是不是回文数,是则输出yes,不是则输出no. 例如12321是回文数,它的个位与万位相同,十位与千位相同. 输入格式: 共一行,为一个5位数. 输出格式: 共一行,y ...
- 005---基于UDP的套接字
基于UDP的套接字 udp不同于tcp协议:不需要经过三次握手.四次挥手.直接发送数据就行. 服务端 import socket ip_port = ('127.0.0.1', 8001) buffe ...
- spring配置jackson不返回null值
#json不返回null spring.jackson.default-property-inclusion=non_null
- (数据科学学习手札21)sklearn.datasets常用功能详解
作为Python中经典的机器学习模块,sklearn围绕着机器学习提供了很多可直接调用的机器学习算法以及很多经典的数据集,本文就对sklearn中专门用来得到已有或自定义数据集的datasets模块进 ...
- Rmarkdown:输出html设置
在Rstudio中可自行更改主题样式 --- title: "题目" author: "name" date: "`r format(Sys.time ...
- [Cracking the Coding Interview] 4.6 Successor 后继节点
Write an algorithm to find the 'next' node(i.e. in-order successor) of a given node in a binary sear ...
- Uber CEO博鳌论坛采访:看好中国市场共享经济的发展模式
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- LeetCode:19. Remove Nth Node From End of List(Medium)
1. 原题链接 https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/ 2. 题目要求 给出一个链表,请 ...
- KMP python实现
首先去 https://blog.csdn.net/starstar1992/article/details/54913261/ 这里看下思想: 然后代码实现,一定要多调试几遍方能看懂: def ge ...