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

Note: 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],
[]
]

分析: 因为有重复的元素,重新模拟组合的情况,首先对于任何结果都含有{}, 然后加入第一个元素1,结果变为{{},{1}}, ,再加入第二个元素2,对原有结果中的两个分别添加进入,{{},{1},{2},{1,2}},再加入2只能为{{},{1},{2},{1,2},{2,2},{1,2,2}} 即第一次加2 添加了两个新组合{2},{1,2} 第二次添加的新组合为{2,2},{1,2,2} 这么看重复的时候就不要重新读取原有结果中的所有元素,在前一个里面直接加好了

class Solution {
public:
int findlen(int value, int i, const vector<int> & nums ){
int len=;
for(int j=i+; j<nums.size()&&(nums[j]==value); j++) len++;
return len; }
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
vector<vector<int>> res;
vector<int> empty;
res.push_back(empty);
sort(nums.begin(),nums.end());
for(int i=; i<nums.size(); ){
int value = nums[i];
int len =;
cout <<"i = "<< i <<endl;
len = findlen(value,i,nums);
cout << len<<endl;
int m =res.size();
for(int t=; t<m; t++){
vector<int> temp=res[t];
for(int j=; j<len; j++){
temp.push_back(value);
res.push_back(temp);
}
}
i+=len; }
return res;
} };

Subsets II的更多相关文章

  1. 42. Subsets && Subsets II

    Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset ...

  2. 【leetcode】Subsets II

    Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...

  3. 90. Subsets II

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

  4. 78. Subsets(M) & 90. Subsets II(M) & 131. Palindrome Partitioning

    78. Subsets Given a set of distinct integers, nums, return all possible subsets. Note: The solution ...

  5. Subsets II - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Subsets II - LeetCode 注意点 有重复的数字 数组可能是无序的,要先排序 解法 解法一:递归,只需要在Subsets中递归写法的基础上 ...

  6. 【LeetCode】90. Subsets II (2 solutions)

    Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...

  7. leetcode 78. Subsets 、90. Subsets II

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

  8. LeetCode解题报告—— Word Search & Subsets II & Decode Ways

    1. Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be con ...

  9. [Leetcode Week8]Subsets II

    Subsets II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/subsets-ii/description/ Description Given ...

  10. Leetcode之回溯法专题-90. 子集 II(Subsets II)

    Leetcode之回溯法专题-90. 子集 II(Subsets II) 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入 ...

随机推荐

  1. ios 开发需要看的书籍

    1.吴航写的<iOS应用逆向工程 第2版> 2.<iOS 应用安全攻防实战> 3.

  2. Android Activity的4种启动模式详解(示例)

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/5233269.html 先介绍下Android对Activity的管理,Android采用Task来管理多个A ...

  3. 读书笔记--SQL必知必会01--了解SQL

    1.1 数据库基础 数据库(datebase) 保存有组织的数据的容器(通常是一个文件或一组文件),是一个以某种有组织的方式存储的数据集合. 数据库管理系统(DBMS,Data Base Manage ...

  4. HTML5学习

    HTML5动画效果 http://www.html5tricks.com/30-more-html5-apps.html http://www.html5tricks.com/category/htm ...

  5. C++_系列自学课程_第_12_课_结构体

    #include <iostream> #include <string> using namespace std; struct CDAccount { double bal ...

  6. MSSQL远程连接

    背景:部署公司自己研发的ERP系统. 1)系统架构: .NET+MSSQL. 2)服务器系统:Windows Server 2008 R2 Enterprise 3)数据库:MSSQL Server ...

  7. div仿textarea使高度自适应

    今天真的有些无语,在百度上找了很多关于textarea和input高度自适应的代码,并且考虑到了要判断textarea的滚动条,从而动态改变它的高度,直到我搜索了这个让我目瞪狗呆的办法…… <d ...

  8. no identity found Command /usr/bin/codesign failed with exit code 1 报错解决方法

    stackoverflow 的解决方法是 xcode->preference->account->view detail -> refresh the provisioning ...

  9. Java之递归求和的两张方法

    方法一: package com.smbea.demo; public class Student { private int sum = 0; /** * 递归求和 * @param num */ ...

  10. vs2013 git 使用总结

    一.参与别人已经建好的项目 方法1.打开VS2013,切换到“团队资源管理器”,点上方“主页”右侧的下拉三角,选择项目->连接到团队项目,然后选择“克隆”,填入Git的Remote Url和要克 ...