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 ...
随机推荐
- Custom Ribbon in SharePoint 2010 & which not wrok when migrate from 2010 to 2013
博客地址 http://blog.csdn.net/foxdave 1. First of all, let me show you the ribbon modal in our project w ...
- wbr 视机而动
链接 在适当的时候, 除非能容下整个单车, 才保留一行: 缩放浏览器, 试试这段就知道了 <p>To learn AJAX, you must be familiar with the X ...
- flex布局在垂直居中里,元素超过容器大小后,不能通过滚动条滚动到顶端,这是个flex的bug
The Problem Flexbox makes centering very easy. By simply applying align-items: center and justify-co ...
- 基于tiny4412的u-boot移植(一)
作者信息 作者:彭东林 邮箱:pengdonglin137@163.com QQ: 405728433 平台介绍 开发环境:win7 64位 + VMware11 + Ubuntu14.04 64位 ...
- 送人玫瑰,手留余香——2015年技术分享交流小结
飞测说:分享让我们更加团结,交流让我们更加凝聚,送人玫瑰,手留余香,更多分享交流也让自己成长的更加完善,2015年已经过去了好几个月,今天刚好整理了下我们科大讯飞武汉测试团队技术分享交流的这块,顺便做 ...
- Web Server部署架构图
一.整体架构图 二.框架的瓶颈 上述框架的瓶颈在存储NFS,现在较多的使用的是GFS分布式存储
- Chrome Developer Tools 中的 Preview 不显示 HTML 的问题
Chrome Developer Tools 中的 Preview 不显示 HTML 的问题 最近升级到 Chrome V64,发现 Chrome Developer Tools 中的 Preview ...
- 【转】让开发变得简单一点- Visual Studio 2010几个让人印象深刻的新功能
原文网址:http://xhinker.blog.51cto.com/640011/313055/ 引言 "我们的目标,不仅仅是做出几个新功能,而是要回答一个问题:'如何让现在的开发人员生活 ...
- node.js + express 初体验【hello world】
[node.js] 一个神奇的XX 呵呵 :) 不知道怎么形容他才好! [express] 是node.js 开发web应用程序的框架 开发环境:XP 大家共同进步吧 :) 一:前期准备: 1:下载 ...
- popup控件代码示例
1.jsp页面input框中的代码 <td class="value"> <input name="demos[0].id" type=&qu ...