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. 【转】Android API 中文(14) —— ViewStub

    用一个XML源填充view.inflate(上下文对象,资源文件Id,父窗口组一般为null): 原文网址:http://www.cnblogs.com/over140/archive/2010/10 ...

  2. STL中,back_insert_iterator与back_inserter的区别。

    1.参考http://www.cplusplus.com网站关于back_insert_iterator与back_inserter的介绍之后,我总算明白了:back_insert_iterator, ...

  3. <php>删除(有内容的)文件夹函数程序

    function deldir($dirname) { if(!file_exists($dirname)) {//判断文件夹是否存在 die("文件夹不存在!");//作用等于( ...

  4. npm 和 bower的区别

    npm和bower在功能上有一定的重合,但不是互斥关系,可以在项目中同时运用.区别在于npm在设计之初就采用了的是嵌套的依赖关系树.一个普通的前端包的依赖树比较长,npm 会将开发环境一起下载下来,  ...

  5. IOS UIlabel设置文本距离边框距离

    自定义UILabel 继承 UILabel 重写drawTextInRect 方法具体如下: CGRect rect = CGRectMake(rect.origin.x + 5, rect.orig ...

  6. vagrant拷贝后vagrant file需要加的配置

    config.ssh.forward_agent config.ssh.username = "vagrant" config.ssh.password = "vagra ...

  7. Java小项目--坦克大战(version1.0)

    Java小项目--坦克大战<TankWar1.0> 这个小项目主要是练习j2se的基础内容和面向对象的思想.项目实现了基本的简单功能,我方一辆坦克,用上下左右键控制移动方向,按F键为发射炮 ...

  8. [Redux] Extracting Presentational Components -- AddTodo

    The code to be refactored: let nextTodoId = 0; class TodoApp extends Component { render() { const { ...

  9. NSLog用法,打印日志

    要输出的格式化占位:   %@ 对象 %d, %i 整数 %u   无符整形 %f 浮点/双字 %x, %X 二进制整数 %o 八进制整数 %zu size_t %p 指针 %e   浮点/双字 (科 ...

  10. spark 高级算子

      mapPartitionsWithIndex val func = (index: Int, iter: Iterator[(Int)]) => {   iter.toList.map(x  ...