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. 80.Vigenère密码(模拟)

    Vigenère密码(文件名vigenere.cpp   vigenere.in    vigenere.out) 题目描述 Description 16 世纪法国外交家Blaise de Vigen ...

  2. Unity快捷键总结

    Shift+Alt+A  物体快速激活 Ctrl+P 开始 Ctrl+Shift+P 暂停 Ctrl+B  编译并运行 Z  Pivot/Center切换 X Local/Global切换

  3. JS取整,四舍五入,取绝对值等Math对象常用方法

    function f1(type,num1) { switch(type) { case 'floor': return Math.floor(num1);//取整或下舍入 break; case ' ...

  4. HDU 4585 Shaolin(水题,STL)

    Shaolin Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Sub ...

  5. CMoReader

    #ifndef __E3GLOGLOADBYFILE_H__ #define __E3GLOGLOADBYFILE_H__ #include "PubCommon\MemoryManager ...

  6. Unable to load DLL 'opencv_core290'

    问题: In my winforms application I need to use some Emgu.CV libraries (I have installed Emgu 2.9). Pro ...

  7. MySQL在windows系统的安装

    原文:https://blog.csdn.net/wokaowokaowokao12345/article/details/76736152 MySQL在windows系统的安装 原创 2017年08 ...

  8. Appium+python自动化15-在Mac上环境搭建

    前言 mac上搭建appium+python的环境还是有点复杂的,需要准备的软件 1.nodejs 2.npm 3.cnpm 4.appium 5.pip 6.Appium-Python-Client ...

  9. 简单的内存缓存模块 - Smache

    介绍 [sm]art + c[ache] = smache Smache 是一个方便的内存缓存模块,可以通过一些简单缓存策略避免无限占用更多的内存,同时确保最常用最应该被缓存的对象被缓存. GitHu ...

  10. Integer IntegerCache源码

    先看一段测试结果: /*public static void main(String[] args) { Integer a = 128, b = 128; Integer c = 127, d = ...