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:
[
[],
[],
[,,],
[,],
[,],
[]
]
DFS :
class Solution {
public:
void DFS(vector<int> &S, vector<int> &temp,int n, int size,int start)
{ if(n == size)
{
result.push_back(temp);
return ;
}
if(n > size)
return ; for(int i = start; i< len ;i++)
{
if(i != start && S[i] == S[i-])
continue ;
if(flag[i] == false)
{
flag[i] = true;
temp.push_back(S[i]);
DFS(S, temp, n+, size,i+);
temp.pop_back();
flag[i] = false;
}
} }
vector<vector<int> > subsetsWithDup(vector<int> &S) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
result.clear();
len = S.size();
flag.resize(len,false);
vector<int> temp;
result.push_back(temp) ;
sort(S.begin(), S.end()); for(int i = ; i <= len ; i++)
DFS(S, temp,, i,); return result;
}
private:
vector<vector<int> > result ;
vector<bool> flag;
int len;
};
解释下这句:”if(i != start && S[i] == S[i-1]) continue ; 这句话保证每个循环进入的元素不会有重复。start : 保证所有的元素进入的顺序为从左往右~
重写后的代码:
class Solution {
public:
void DFS(vector<int> &S, vector<int> &ans, int size, int currentPos)
{ if(ans.size() == size ){
res.push_back(ans);
return;
} for(int i = currentPos; i< S.size(); ++i)
{
if(i != currentPos && S[i] == S[i-]) continue;
ans.push_back(S[i]);
DFS(S, ans, size, i+);
ans.pop_back();
}
}
vector<vector<int> > subsetsWithDup(vector<int> &S) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
res.clear();
sort(S.begin(), S.end());
vector<int> emp;
res.push_back(emp); for(int i = ; i <= S.size(); ++i)
{
vector<int> ans;
DFS(S, ans, i, );
} return res;
}
private:
vector<vector<int>> res;
};
LeetCode_Subsets II的更多相关文章
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 117 - Populating Next Right Pointers in Each Node II
题目链接:Populating Next Right Pointers in Each Node II | LeetCode OJ Follow up for problem "Popula ...
- 函数式Android编程(II):Kotlin语言的集合操作
原文标题:Functional Android (II): Collection operations in Kotlin 原文链接:http://antonioleiva.com/collectio ...
- 统计分析中Type I Error与Type II Error的区别
统计分析中Type I Error与Type II Error的区别 在统计分析中,经常提到Type I Error和Type II Error.他们的基本概念是什么?有什么区别? 下面的表格显示 b ...
- hdu1032 Train Problem II (卡特兰数)
题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能. (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...
- [LeetCode] Guess Number Higher or Lower II 猜数字大小之二
We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...
- [LeetCode] Number of Islands II 岛屿的数量之二
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
随机推荐
- 面试题 46 1+ 2+3+...+n
class Temp{ public: Temp(){ ++N; sum+=N; } static void Reset(){ N = ; sum = ; } static int getSum(){ ...
- LeetCode_Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- Smartcard CA智能卡之调试
Integrated Circuit Card 集成电路卡,也叫CA卡或智能卡,将一个微电子芯片嵌入符合ISO 7816标准的卡基内,做成卡片形式,也是一个嵌入式小系统.由CPU,ROM,RAM及E ...
- 深入理解7816(3)-----关于T=0
卡片和终端之间的数据传输是通过命令响应的方式进行的,卡片只能被动地接收命令,并且给出响应.所有的命令都是以命令头开始,而该命令被完整地执行后(无论结果对错),必须以包含状态字(SW1 SW2)的响应结 ...
- 自制单片机之十三……时钟IC_DS1302
在网上看了很久,发现初学者最有兴趣的就是DS1302时钟电路,也很自然,它是个做出来就让你觉得最实用的电路了,但实际上制做上并不简单,首先你要让你的显示部分(不管是数码管还是LCD)调试通过.然后把D ...
- Linux cat和EOF的使用
在某些场合,可能我们需要在脚本中生成一个临时文件,然后把该文件作为最终文件放入目录中.(可参考ntop.spec文件)这样有几个好处,其中之一就是临时文件不是唯一的,可以通过变量赋值,也可根据不同的判 ...
- Linux 文件名匹配
As the shell reads each line, it "handles" any special characters. This includes variable ...
- TranslateAnimation详解
TranslateAnimation详解 Android JDK为我们提供了4种动画效果,分别是: AlphaAnimation,RotateAnimation, ScaleAnimation, Tr ...
- logstash 发送zabbix告警
<pre name="code" class="html">[elk@dr-mysql01 test]$ cat t1.conf input { s ...
- Abstract Methods and Classes
阅读了Java的官方Doc,在此总结如下. Abstract Class & Method In jave, "abstract" can be a modifier to ...