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. C8051F学习笔记:单片机的驱动能力

    学习51单片机的时候我们就知道51单片机的I/O口的特点:P0口没有弱上拉,所以做地址线时不用上拉,但输出“1”时就要加上拉电阻,不然输出电平到不了高电平,P1~P3则不存在这个问题,每个输出管脚都有 ...

  2. IEqualityComparer<T>接口

    IEqualityComparer<T>接口的对象的主要作用在于自定义判断两个对象是否相等. 其中最常用的方法: bool Equals(T x, T y); 实现该方法用于比较两个对象是 ...

  3. 【Xamarin开发IOS-IOS生命周期】

    iOS的应用程序的生命周期,还有程序是运行在前台还是后台,应用程序各个状态的变换,这些对于开发者来说都是很重要的. iOS系统的资源是有限的,应用程序在前台和在后台的状态是不一样的.在后台时,程序会受 ...

  4. Android 编程:calledfromWrongThreadException 的原因

    子线程更新UI会发生Android.view.ViewRoot$CalledFromWrongThreadException异常的解决方法 子线程更新UI 显然假如你的程序需要执行耗时的操作的话,假如 ...

  5. Linux 删除空行

    在Linux上处理一些数据文件时,有时候需要将其中的空行过滤掉,系统中提供的各种工具都可以完成这个功能.将常用的介绍如下吧:1. grep grep . data.txt grep -v '^$' d ...

  6. hsql使用架构包启动数据库

    一.通常我们平时启动就是直接通过hsql.jar来进行启动 java -cp hsqldb.jar org.hsqldb.util.DatabaseManagerSwing java -cp hsql ...

  7. 设置MATLAB中figure的背景为白色

    matlab的图形窗口每次背景都是灰色的,而我希望每次都是白色的背景,方便用图: 每次总是需要添加figure('color','w');或者figure('color',[1 1 1])或者set( ...

  8. Cuts the cake_hdu_2134.java

    Cuts the cake Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  9. Android 自定义UI--电池

    首先看一下效果图, 下面看代码: /** * */ package com.example.batterydemo; import android.content.Context; import an ...

  10. Android 自定义控件 优雅实现元素间的分割线 (支持3.0以下)

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/42407923 ,本文出自:[张鸿洋的博客] 1.概述 话说,随着Android ...