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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 函数式Android编程(II):Kotlin语言的集合操作

    原文标题:Functional Android (II): Collection operations in Kotlin 原文链接:http://antonioleiva.com/collectio ...

  4. 统计分析中Type I Error与Type II Error的区别

    统计分析中Type I Error与Type II Error的区别 在统计分析中,经常提到Type I Error和Type II Error.他们的基本概念是什么?有什么区别? 下面的表格显示 b ...

  5. hdu1032 Train Problem II (卡特兰数)

    题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能.    (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...

  6. [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 ...

  7. [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 ...

  8. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  9. [LeetCode] Permutations II 全排列之二

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

随机推荐

  1. eclipse js中 选中方法按F3快捷键不能跳转到对应方法的解决方案

    这种情况很可能是m2e-wtp插件没有安装的,安装插件成功后即可解决. m2e-wtp插件安装参照相应随笔.

  2. 【Eclipse DDMS】 Can't bind to local 8600 for debugger

    问题原因: 电脑上同时安装了Eclipse 和Android Studio两个ide. 关键是使用eclipse adb连接真机时候,android studio也处于运行状态,后者默认也是要连接ad ...

  3. python高级编程技巧

    由python高级编程处学习 http://blog.sina.com.cn/s/blog_a89e19440101fb28.html Python列表解析语法[]和生成 器()语法类似 [expr  ...

  4. Android开发之DatePickerDialog与TimePickerDialog的功能和使用方法具体解释

    DatePickerDialog与TimePickerDialog的功能比較简单,使用方法也非常easy.仅仅要以下两步就可以. Ø  通过newkeyword创建DatePickerDialog.T ...

  5. ViewPager顶部标题控件PagerSlidingTabStrip

    最近搞一个项目,要求做一个和网易新闻顶部菜单的滑动效果,如图: 顶部标题中下面有个红色的矩形小条,左右滑动时会跟随手势动态滑动,效果很绚丽,唉,特效啊! 自己搞了一上午无果,还是是github上找大神 ...

  6. AVL旋转树

    执行插入操作可能出现不平衡的情况,当平衡二叉树.AVL这树是一种自平衡二叉树,使二叉树又一次保持平衡.而且查找.插入和删除操作在平均和最坏情况下时间复杂度都是O(log n) AVL树的旋转一共同拥有 ...

  7. C++11中正則表達式測试

    VC++2010已经支持regex了, 能够用来编译下述代码. #include <string> #include <regex> #include <iostream ...

  8. (转)sql中 in 、not in 、exists、not exists 用法和差别

    exists (sql 返回结果集为真)  not exists (sql 不返回结果集为真)  如下:  表A  ID NAME  1    A1  2    A2  3  A3 表B  ID AI ...

  9. Palindrome Number 回文数

    判断一个数字是否是回文数,尝试不用其他额外空间. 注意: 负数也有可能成为回文数吗? 如果你想让int转为string,注意不用其他空间这个约束. 你也可以翻转一个int,但是有可能会溢出.     ...

  10. 使用hibernate优化

    a. 在查询字符串中,应该总是使用jdbc的占位符?,或使用使用命名参数:,不要自查询中使用字符串值来代替非常量值.   b.  Flush会影响性能,频繁刷新影响性能,尽量减少不必要的刷新.   c ...