90. Subsets II (Back-Track, DP)
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)的更多相关文章
- leetcode 78. Subsets 、90. Subsets II
第一题是输入数组的数值不相同,第二题是输入数组的数值有相同的值,第二题在第一题的基础上需要过滤掉那些相同的数值. level代表的是需要进行选择的数值的位置. 78. Subsets 错误解法: cl ...
- 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 ...
- 90. Subsets II
题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...
- 【LeetCode】90. Subsets II (2 solutions)
Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...
- 【LeetCode】90.Subsets II
Subsets II Given a collection of integers that might contain duplicates, nums, return all possible s ...
- LeetCode Problem 90. Subsets II
python solution 123456789101112131415161718192021222324252627 class (object): def subsetsWithDup(sel ...
- 78. Subsets 90. Subsets II
1. Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset m ...
- Leetcode#90 Subsets II
原题地址 跟Subsets(参见这篇文章)类似. 但因为有重复元素,所以要考虑去重问题. 什么情况下会出现重复呢?比如S = {5, 5, 5},如果要选1个5,一共有C(3,1)=3种选法,即100 ...
- LeetCode 90. Subsets II (子集合之二)
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
随机推荐
- Android 4.3发布 新增4大改变25日推送升级[附Android 4.3 工厂镜像]
北京时间7月25日,谷歌举行发布会,正式发布了全新的Nexus 7平板电脑以及Android 4.3系统. 其中Android 4.3系统隶属于4.X果冻豆(Jelly Bean)系列,是目前最新的操 ...
- Alpha阶段第1周/共2周 Scrum立会报告+燃尽图 03
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2248 一.小组介绍 组长:刘莹莹 组员:朱珅莹 孙韦男 祝玮琦 王玉潘 ...
- New Concept English Two 31 85
$课文83 大选之后 904. The former Prime Minister, Mr. Wentworth Lane, was defeated in the recent elections ...
- 基于tiny4412的u-boot移植(一)
作者信息 作者:彭东林 邮箱:pengdonglin137@163.com QQ: 405728433 平台介绍 开发环境:win7 64位 + VMware11 + Ubuntu14.04 64位 ...
- 深入了解 Session 与 Cookie
Java —— 深入了解 Session 与 Cookie Java web 了解Cookie和Session 定义 1.很通俗的来讲,Cookie就是浏览器的缓存,就是用户访问网站时保存在浏 ...
- 最新解决 Ubuntu16.04 和 win10 双系统时间同步问题 (设置为 UTC 时间)
最近在电脑上安装了 Ubuntu16.04 和 Win10 双系统, 开机后发现电脑的开机系统出现了问题,不知道怎么搞的总是会出现8个小时的误差,在网上查了好多文章发现网上的大部分方法都是比较过时的 ...
- python try except, 异常处理
http://www.runoob.com/python/python-exceptions.html http://blog.sciencenet.cn/blog-3031432-1059523.h ...
- python pandas.DataFrame .loc,.iloc,.ix 用法
refer to: http://www.cnblogs.com/harvey888/p/6006200.html
- 嵌入式linux开发:杂七杂八的话
1. 编译器:常用的有Sourcery,linaro等.当然芯片厂家一般也会提供可用的编译器. 2. 将配置和编译时的输出O到其他位置:make O=some_dir 这样做的好处是保持源代码树的干净 ...
- struts2学习(2)struts2核心知识
一.Struts2 get/set 自动获取/设置数据 根据上一章.中的源码继续. HelloWorldAction.java中private String name,自动获取/设置name: pac ...