解题思路:

要生成子集,对于vector 中的每个数,对于每个子集有两种情况,加入或不加入。

因此代码:

class Solution {
public:
void subsetG(vector<int> nums, vector<vector<int>>& result, vector<int> temp, int c)
{
if(c>=nums.size())
{
result.push_back(temp);
return ;
}
int i=c;
subsetG(nums,result, temp,i+1);
temp.push_back(nums[i]);
subsetG(nums,result, temp,i+1); } vector<vector<int>> subsetsWithDup(vector<int>& nums) { vector<vector<int>> result;
vector<int> temp;
subsetG(nums, result,temp,0); return result; }
};

 

 但是这样会造成许多重复的子集,因为重复的情况我们只需要考虑一次.可以分为拿和不拿. 如果拿的话就按照正常往下一层搜索, 如果不拿当前值的话, 那么也要跳过接下来和当前值相等的元素.

class Solution {
public:
void subsetG(vector<int> nums, vector<vector<int>>& result, vector<int> temp, int c)
{
if(c>=nums.size())
{
result.push_back(temp);
return ;
}
int i=c;
while(c+1<nums.size()&&nums[i]==nums[c+1])
{
c++;
}
subsetG(nums,result, temp,c+1); temp.push_back(nums[i]); subsetG(nums,result, temp,i+1); } vector<vector<int>> subsetsWithDup(vector<int>& nums) { vector<vector<int>> result;
vector<int> temp;
subsetG(nums, result,temp,0); return result; }
};

  

leetcode 90. subsets的更多相关文章

  1. [LeetCode] 90.Subsets II tag: backtracking

    Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...

  2. [leetcode]90. Subsets II数组子集(有重)

    Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...

  3. [LeetCode] 90. Subsets II 子集合 II

    Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...

  4. LeetCode 90. Subsets II (子集合之二)

    Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...

  5. [LeetCode] 90. Subsets II 子集合之二

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...

  6. Leetcode#90 Subsets II

    原题地址 跟Subsets(参见这篇文章)类似. 但因为有重复元素,所以要考虑去重问题. 什么情况下会出现重复呢?比如S = {5, 5, 5},如果要选1个5,一共有C(3,1)=3种选法,即100 ...

  7. leetCode 90.Subsets II(子集II) 解题思路和方法

    Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...

  8. LeetCode Problem 90. Subsets II

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

  9. leetcode 78. Subsets 、90. Subsets II

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

随机推荐

  1. snmpwalk

    什么是snmpwalk?snmpwalk是一个SNMP小程序,它使用SNMP的GETNEXT请求查询指定OID(SNMP协议中的对象标识)入口的所有OID树信息,并显示给用户. snmpwalk的作用 ...

  2. Codeforces Round #523 (Div. 2)

    Codeforces Round #523 (Div. 2) 题目一览表 来源 考察知识点 完成时间 A Coins cf 贪心(签到题) 2018.11.23 B Views Matter cf 思 ...

  3. 【NOI2013模拟】坑带的树(仙人球的同构+圆方树乱搞+计数+HASH)

    [NOI2013模拟]坑带的树 题意: 求\(n\)个点,\(m\)条边的同构仙人球个数. \(n\le 1000\) 这是一道怎么看怎么不可做的题. 这种题,肯定是圆方树啦~ 好,那么首先转为广义圆 ...

  4. HTML学习笔记Day10

    一.Form表单补充(收集用户信息) 1.button按钮(不进行提交) 1)语法1:<input type="button" value="按钮内容" ...

  5. day09-(servlet)

    回顾: xml: 可扩展的标签语言 标签可以自定义 作用:配置文件 xml组成: 声明 首行顶格写 元素 <xxx></xxx> <xx/> 属性 <xxx ...

  6. Nginx+Keeplived双机热备(主从模式)

    Nginx+Keeplived双机热备(主从模式) 参考资料: http://www.cnblogs.com/kevingrace/p/6138185.html 双机高可用一般是通过虚拟IP(漂移IP ...

  7. httprouter使用pprof

    httprouter使用pprof 参考:https://github.com/feixiao/httpprof 性能分析参考:https://github.com/caibirdme/hand-to ...

  8. MySQL数据类型2

    一.MySQL的数据类型 主要包括以下五大类: 整数类型:BIT.BOOL.TINY INT.SMALL INT.MEDIUM INT. INT. BIG INT 浮点数类型:FLOAT.DOUBLE ...

  9. 解决composer出错的原因

    1.执行了php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');" 2.出 ...

  10. Springboot中Feign的使用总结

    Feign是Webservice服务的客户端,创建接口+注解就可完成,实现简单 客户端通过@EnableFeignClients开启Feign的支持功能 @SpringBootApplication ...