Given a set of distinct integers, nums, 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 nums = [1,2,3], a solution
is:

[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]

分析:

的题目意思基本一样,主要差别在于还须要对不同的len的子数组进行回溯,所以回溯法终于确定答案的方式不一样。

(本题是用len作为回溯时确定答案的根据)

class Solution {
public:
void dfs(vector<int>& nums, vector<int> &subres, int start, int len)//使用引用,有利于防止内存大爆炸
{
if (subres.size() == len)//已经获得答案。而且回溯
{
result.push_back(subres);
return;//回溯
}
for (int i = start; i < nsize; i++)
{
subres.push_back(nums[i]);
dfs(nums, subres, i + 1, len);
subres.pop_back(); // 完毕一个解后去掉末尾元素 ,准备下一次回溯寻找答案
}
}
vector<vector<int>> subsets(vector<int>& nums) {
nsize=nums.size();
if ( nsize == 0)
return result;
sort(nums.begin(),nums.end()); //一,先排序
vector<int> subres;
for(int len=0; len<=nums.size() ;len++)//二,对不同长度的子数组进行回溯
dfs(nums, subres, 0, len);
return result;
} private:
vector<vector<int > > result;
int nsize;
};
Total Accepted: 62569 Total
Submissions: 208285 Difficulty: Medium

Given a collection of integers that might contain duplicates, nums, 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 nums = [1,2,2], a solution
is:

[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]

分析:

比上面的代码多加入一个同样组合不要的回溯条件就可以,只是代码的速度尽然打败了0.89%,也就是说极慢的速度。感觉不会再爱了。

class Solution {
public:
void dfs(vector<int>& nums, vector<int> &subres, int start, int len)//使用引用,有利于防止内存大爆炸
{
if (subres.size() == len)//已经获得答案。而且回溯
{
if(!isSameVec(subres)) //已经出现过的组合不要。
result.push_back(subres);
return;//回溯
}
for (int i = start; i < nsize; i++)
{
subres.push_back(nums[i]);
dfs(nums, subres, i + 1, len);
subres.pop_back(); // 完毕一个解后去掉末尾元素 。准备下一次回溯寻找答案
}
}
bool isSameVec(vector<int> &sub)
{
for(int i=0;i<result.size();i++)
{
if(result[i]==sub)
return true;
}
return false;
}
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
nsize=nums.size();
if ( nsize == 0)
return result;
sort(nums.begin(),nums.end()); //一,先排序
vector<int> subres;
for(int len=0; len<=nums.size() ;len++)//二,对不同长度的子数组进行回溯
dfs(nums, subres, 0, len);
return result;
}
private:
vector<vector<int > > result;
int nsize;
};

学习别人的算法:

class Solution {
public:
void dfs(vector<int> &s, int index, vector<int> &subset, vector<vector<int>> &res)
{
res.push_back(subset);
for(int i = index; i< s.size(); i++)
{
if(i!=index && s[i]==s[i-1])
continue;
subset.push_back(s[i]);
dfs(s,i+1,subset,res);
subset.pop_back();
}
}
vector<vector<int> > subsetsWithDup(vector<int> &S) {
// Note: The Solution object is instantiated only once.
vector<vector<int>> result;
sort(S.begin(),S.end());
vector<int> subset;
dfs(S,0,subset,result);
return result;
}
};

注:本博文为EbowTang原创,兴许可能继续更新本文。假设转载。请务必复制本条信息!

原文地址:http://blog.csdn.net/ebowtang/article/details/50844984

原作者博客:http://blog.csdn.net/ebowtang

本博客LeetCode题解索引:http://blog.csdn.net/ebowtang/article/details/50668895

&lt;LeetCode OJ&gt; 78 / 90 Subsets (I / II)的更多相关文章

  1. LeetCode OJ 95. Unique Binary Search Trees II

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  2. LeetCode OJ Remove Duplicates from Sorted Array II

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  3. LeetCode OJ:Search a 2D Matrix II(搜寻二维矩阵)

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  4. LeetCode OJ :Unique Binary Search Trees II(唯一二叉搜索树)

    题目如下所示:返回的结果是一个Node的Vector: Given n, generate all structurally unique BST's (binary search trees) th ...

  5. leetcode 78. Subsets 、90. Subsets II

    第一题是输入数组的数值不相同,第二题是输入数组的数值有相同的值,第二题在第一题的基础上需要过滤掉那些相同的数值. level代表的是需要进行选择的数值的位置. 78. Subsets 错误解法: cl ...

  6. LeetCode Problem 90. Subsets II

    python solution 123456789101112131415161718192021222324252627 class (object): def subsetsWithDup(sel ...

  7. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  8. LeetCode OJ:Integer to Roman(转换整数到罗马字符)

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

  9. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

随机推荐

  1. [CODECHEF]EASYEX

    题意:有一个$k$面的骰子,上面的数字为$1\cdots k$,现在要丢$n$次骰子,设$n$次中有$a_i$次扔到数字$i$,给定$l,f$,求$\prod\limits_{i=1}^la_i^f$ ...

  2. Laravel输出JSON时设定输出字段的几种情况总结

    1.如果输出json的时候需要屏蔽某些字段,或则想自定义显示的字段: 1.model里面设置 protected $hidden = ['password'];//要屏蔽的字段 2.model里面设置 ...

  3. Boost汉字匹配 -- 宽字符

      原文链接:http://blog.csdn.net/sptoor/article/details/4930069 思路:汉字匹配,把字符都转换成宽字符,然后再匹配. 需要用到以下和宽字符有关的类: ...

  4. VK Cup 2016 - Qualification Round 2 C. Road Improvement dfs

    C. Road Improvement 题目连接: http://www.codeforces.com/contest/638/problem/C Description In Berland the ...

  5. SVN 服务器搭建及使用 一

    SVN服务器搭建和使用(一) Subversion是优秀的版本控制工具,其具体的的优点和详细介绍,这里就不再多说. 首先来下载和搭建SVN服务器. 现在Subversion已经迁移到apache网站上 ...

  6. http://www.tangible-engineering.com/tangible_t4editor.html

    T4 Template Tutorials   http://www.tangible-engineering.com/tangible_t4editor.html

  7. ADC Power Supplies

    http://www.planetanalog.com/author.asp?section_id=3041&doc_id=563055 Jonathan Harris, Product Ap ...

  8. 64位系统下同时使用64位和32位的eclipse

    eclipse.ini 文件使用说明 The -vm option and its value (the path) must be on separate lines. The value must ...

  9. (转)媒体格式分析之flv -- 基于FFMPEG

    本来是应该先写一个媒体文件格式的简单讲解的,还没来得及写,以后再写.今天就先根据ffmpeg的flv.c的flv_demux这个结构体来讲解一下当前比较流行的媒体格式flv. FLV 是FLASH V ...

  10. Linux使用jstat命令查看jvm的GC情况(转)

    B. jstack jstack主要用来查看某个Java进程内的线程堆栈信息.语法格式如下: 1 jstack [option] pid 2 jstack [option] executable co ...