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 ...
随机推荐
- 古董留念 - Microsoft Office 4.2中文版
Office 4.2是Office 95的前一个版本,最适合运行在Windows 3.x上,但即使是最新的Windows 7 32位版也是可以安装它的(不信你可以试试)! 原版以软盘为载体,安装一次需 ...
- oracle创建触发器
create or replace trigger 触发器名字 触发时间 触发事件 after(before) insert or update or delete on 表名 for each r ...
- flex布局在垂直居中里,元素超过容器大小后,不能通过滚动条滚动到顶端,这是个flex的bug
The Problem Flexbox makes centering very easy. By simply applying align-items: center and justify-co ...
- linux find查找并拷贝 exec xargs区别
-exec 1.参数是一个一个传递的,传递一个参数执行一次rm 2.文件名有空格等特殊字符也能处理-xargs 1.一次将参数传给命令,可以使用-n控制参数个数 2.处理特殊 ...
- 认识 ARM、FPGA
0. ARM ARM:Advanced RISC machine,微处理行业的一家知名企业.适用于多种领域,如嵌入控制,消费.教育类多媒体.DSP和移动式应用. 优势: 功耗低,不容易发热.死机: 3 ...
- Java第四次作业--面向对象高级特性(继承和多态)
一.学习要点 认真看书并查阅相关资料,掌握以下内容: 掌握类的继承概念和设计 掌握构造方法的继承原则 掌握方法重写 掌握super键字和final关键字 理解多态的概念,掌握通过方法重写和方法重载机制 ...
- Linux 定制X86平台操作系统
/********************************************************************************* * Linux 定制X86平台操作 ...
- LG4777 【模板】扩展中国剩余定理(EXCRT)
题意 题目描述 给定\(n\)组非负整数\(a_i, b_i\),求解关于\(x\)的方程组 \[\begin{cases} x \equiv b_1\ ({\rm mod}\ a_1) \\ x\e ...
- PAT 1021 个位数统计 C语言
1021. 个位数统计 (15) 给定一个k位整数N = dk-1*10k-1 + ... + d1*101 + d0 (0<=di<=9, i=0,...,k-1, dk-1>0) ...
- equals方法和==的区别
equals方法和==的区别 首先大家知道,String既可以作为一个对象来使用,又可以作为一个基本类型来使用.这里指的作为一个基本类型来使用只是指使用方法上的,比如String s = &quo ...