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 ...
 
随机推荐
- ASP.NET WEB API 如何使用基于Post的方式传递多个值(二)
			
前面我曾经写过一篇文章,是基于HttpContext的请求上下文中读取表单参数,其实还可以将其单独拆分出来. 基于Filter的方式 获取表单值:(核心代码) public void OnActi ...
 - bzoj1676[Usaco2005 Feb]Feed Accounting 饲料计算
			
Description Farmer John is trying to figure out when his last shipment of feed arrived. Starting wit ...
 - CSS flex让所有灵活的项目都带有相同的长度,忽略它们的内容:
			
.flexbox { display: -webkit-box; display: -webkit-flex; display: -ms-flexbox; displa ...
 - mysql 获取当前时间戳
			
mysql 获取当前时间为select now() 运行结果: 2012-09-05 17:24:15 mysql 获取当前时间戳为select unix_timestamp(now()) 运行结 ...
 - web工程调用hadoop集群1.2
			
本实例代码在lz的资源中有上传,有需要的可以参考(下载后的文件解压后有两个,一个直接导入myeclipse工程,另外的jar放在hadoop的lib下面,只需修改Utils中的ip即可运行该程序): ...
 - [RxJS] Reactive Programming - Rendering on the DOM with RxJS
			
<!DOCTYPE html> <html> <head> <script src="https://code.jquery.com/jquery- ...
 - iOS中的界面多选功能--(UICollectionView)
			
文/Jacob_Pan(简书作者)原文链接:http://www.jianshu.com/p/9d28ebd0f5a2著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 最近做项目接触了一 ...
 - Hacker(24)----防范密码被轻易破解
			
无论什么类型密码,用户在设置时都有非常小心,防止自己设置的密码被他人轻易破解.为保护重要的文件和资料,可采用加密工具进行加密,即可选择Win7系统自带的BitLocker,也可使用Internet中很 ...
 - 安装oracle11g未找到文件WFMLRSVCApp.ear文件
			
win7_64位系统,安装oracle11gR2时,报错提示: 未找到文件...WFMLRSVCApp.ear文件 解决方法如下: 将下载的两个压缩包解压至同一目录(合并)再安装即可解决此类问题.
 - [CSAPP笔记][第二章信息的表示和处理]
			
信息的表示和处理 2.1 信息存储 机器级程序将存储器视为一个非常大的字节数组,称为虚拟存储器. 存储器的每个字节由一个唯一的数字表示,称为它的地址 所有可能地址的集合称为虚拟地址空间 2.1.1 十 ...