Given a collection of integers that might contain duplicates, nums, 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 nums = [1,2,2], a solution is:

[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]

思路: 对于重复了n次的字符,可以选择放入0,1,2...n个

class Solution {
public:
vector<vector<int> > subsetsWithDup(vector<int> &S) {
vector<vector<int>> result;
vector<int> pre;
if(S.size()==)
return result;
sort(S.begin(),S.end());
result.push_back(pre);
dfs(S,result,pre,);
return result;
}
void dfs(vector<int> &S , vector<vector<int>> &result ,vector<int> pre , int depth)
{
if(depth == S.size()) return; //teminate condition int dupCounter = ;
int dupNum = ;
while(depth+ < S.size() && S[depth] == S[depth+]) //get duplicate times
{
depth++;
dupNum++;
}
while(dupCounter++ <= dupNum) //push duplicate elements
{
pre.push_back(S[depth]);
result.push_back(pre);
dfs(S,result,pre,depth+);
}
dupCounter = ;
while(dupCounter++ <= dupNum) //backtracking
{
pre.pop_back();
}
dfs(S, result,pre, depth+); //push none, dfs directly
}
};

思路II:DP,插入排序法增加元素。重复的元素要在一个for循环内插入,否则会导致subset有重复。

class Solution {
public:
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
vector<vector<int>> ret;
vector<int> retItem;
ret.push_back(retItem);
int size; //number of memebers in ret
int count = ; //count the duplicate number sort(nums.begin(),nums.end()); for(int i = ; i < nums.size(); i++){ //iterate the number to insert
if(i < nums.size()- && nums[i+]==nums[i]){
count++;
continue;
} size = ret.size();
for(int j = ; j < size; j++){ //iterate current item in ret
vector<int> newItem = ret[j];
for(int k = ; k < count; k++){ //duplicate 1,2,...,count times
newItem.push_back(nums[i]);
ret.push_back(newItem);
}
}
count = ;
}
return ret;
}
};

90. Subsets II (Back-Track, DP)的更多相关文章

  1. leetcode 78. Subsets 、90. Subsets II

    第一题是输入数组的数值不相同,第二题是输入数组的数值有相同的值,第二题在第一题的基础上需要过滤掉那些相同的数值. level代表的是需要进行选择的数值的位置. 78. Subsets 错误解法: cl ...

  2. 78. Subsets(M) & 90. Subsets II(M) & 131. Palindrome Partitioning

    78. Subsets Given a set of distinct integers, nums, return all possible subsets. Note: The solution ...

  3. 90. Subsets II

    题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...

  4. 【LeetCode】90. Subsets II (2 solutions)

    Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...

  5. 【LeetCode】90.Subsets II

    Subsets II Given a collection of integers that might contain duplicates, nums, return all possible s ...

  6. LeetCode Problem 90. Subsets II

    python solution 123456789101112131415161718192021222324252627 class (object): def subsetsWithDup(sel ...

  7. 78. Subsets 90. Subsets II

    1. Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset m ...

  8. Leetcode#90 Subsets II

    原题地址 跟Subsets(参见这篇文章)类似. 但因为有重复元素,所以要考虑去重问题. 什么情况下会出现重复呢?比如S = {5, 5, 5},如果要选1个5,一共有C(3,1)=3种选法,即100 ...

  9. LeetCode 90. Subsets II (子集合之二)

    Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...

随机推荐

  1. BigDecimal 、BigInteger

    package com.BigDecimal; public class BigDecimalDemo { /* * 下面的运算的结果出乎我们的意料,有些准确,有些不准确 * 这是为什么呢? * 我们 ...

  2. 爱奇艺、腾讯、优酷、搜狐、芒果、乐视、PPTV、音悦台等VIP视频免费观看

    观看地址一:http://www.luoruiyuan.cn/pages/id-62_uid-2_btid-35.html 观看地址二:http://movie.luoruiyuan.cn/vip.h ...

  3. New Concept English Two 3

    $课文5 无错号之虞 47. Mr.James Scott has a garage in Silbury and now he has just bought another garage in P ...

  4. php 递归读取目录

    看到很多面试题有这个,今天有机会写了一下. 要注意的是: 在opendir这个函数用完后,要注意closedir,因为安全问题,打开的目录依然存在于内存中,在并发情况下最好关闭,不然容易被破坏. &l ...

  5. NYOJ-1036 非洲小孩

    非洲小孩 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描写叙述 家住非洲的小孩,都非常黑.为什么呢? 第一,他们地处热带,太阳辐射严重. 第二,他们不常常洗澡.(常年缺水, ...

  6. QQ空间点赞代码

    jQuery("a.qz_like_btn_v3[data-clicklog='like']").each(function(index,item){ console.log(it ...

  7. Web验证方式(2)--Form Authentication

    Form验证方式并不是HTTP标准,而是在微软ASP.NET Web框架下提供的一种验证方式.其大致流程如下: 在上图的流程中,ASP.NET框架提供了如下支持类:( FormsAuthenticat ...

  8. laravel 控制器中使用 try catch

    需要操作数据库时,当数据字段不一致,mysql报错,控制程序,需要使用try catch 下面是使用案例 $morder['morder_time'] = time();//在这里使用try catc ...

  9. 模仿VIMD的模式的简化代码示例

    按numpad0来切换模式,按t显示不同的结果: Numpad0:: tfmode:=!tfmode aaa:=(tfmode=?"AAAA":"BBBB") ...

  10. charles手机抓包配置-2

    破解的安装包,自己的百度云里有收藏 http://www.zhimengzhe.com/IOSkaifa/248398.html http://blog.csdn.net/swj6125/articl ...