题目链接

Subsets II - LeetCode

注意点

  • 有重复的数字
  • 数组可能是无序的,要先排序

解法

解法一:递归,只需要在Subsets中递归写法的基础上多加一句if(find(ret.begin(),ret.end(),tmp) == ret.end()) ret.push_back(tmp);j即可,因为已经排序了,所以加进去的如果已经存在就说明是重复的。

class Solution {
public:
typedef vector<int> v;
void recursion(int start,v nums,v tmp, vector<v>& ret)
{
if(find(ret.begin(),ret.end(),tmp) == ret.end()) ret.push_back(tmp);
int n = nums.size();
for(int i = start;i < n;i++)
{
tmp.push_back(nums[i]);
recursion(i+1,nums,tmp,ret);
tmp.pop_back();
}
}
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
sort(nums.begin(),nums.end());
vector<vector<int>> ret;
v tmp;
recursion(0,nums,tmp,ret);
return ret;
}
};



解法二:递归。只需要在Subsets中递归写法的基础上多加一句while(i+1 < n && nums[i] == nums[i+1]) i++;即可,这句话的作用就是把下图中树结点为X的剪枝。

                        []
/ \
/ \
/ \
[1] []
/ \ / \
/ \ / \
[1 2] [1] [2] []
/ \ / \ / \ / \
[1 2 2] [1 2] X [1] [2 2] [2] X []
class Solution {
public:
typedef vector<int> v;
void recursion(int start,v nums,v tmp, vector<v>& ret)
{
ret.push_back(tmp);
int n = nums.size();
for(int i = start;i < n;i++)
{
tmp.push_back(nums[i]);
recursion(i+1,nums,tmp,ret);
tmp.pop_back();
while(i+1 < n && nums[i] == nums[i+1]) i++;
}
}
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
sort(nums.begin(),nums.end());
vector<vector<int>> ret;
v tmp;
recursion(0,nums,tmp,ret);
return ret;
}
};

解法三:非递归。在Subsets中非递归写法的基础上进行修改,如果当前数字和上一次循环的数字一样,那么就只能在上一次循环产生的集合后面追加当前数字,而不能在所有集合后面追加

class Solution {
public:
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
if(nums.empty()) return {};
sort(nums.begin(),nums.end());
vector<vector<int>> ret(1);
int i,j,n = nums.size(),last = nums[0],lastSize = 1;
for(i = 0;i < n;i++)
{
if(last != nums[i])
{
last = nums[i];
lastSize = ret.size();
}
int m = ret.size();
for(j = m-lastSize;j < m;j++)
{
ret.push_back(ret[j]);
ret.back().push_back(nums[i]);
}
}
return ret;
}
};

小结

Subsets II - LeetCode的更多相关文章

  1. Subsets II ——LeetCode

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

  2. 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 ...

  3. [Leetcode Week8]Subsets II

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

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

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

  5. 【leetcode】Subsets II

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

  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】90.Subsets II

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

  9. 90. Subsets II

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

随机推荐

  1. 新手Python第一天(接触)

    Python 变量 Python的变量由字母,数字,下划线组成不包含特殊字符,不能以数字开头 可以使用的名称 例如:name,name2,my_name 不可使用的名称 例如:if...(Python ...

  2. 打包应用和构建Docker镜像(docker在windows上)

    在构建Docker时编译应用 一般有两种方法在构建镜像时进行打包应用.第一种方法就是使用基本的镜像,该镜像包括应用平台和构建工具,因此在Dockerfile中,复制源代码到镜像中并在构建镜像时编译ap ...

  3. Plasma Cash 合约解读

    作者介绍 虫洞社区·签约作者 steven bai Plasma Cash 合约解读 Plasma Cash 合约解读 1. 合约代码 2. 合约文件简单介绍 3. Plasma Cash 的基础数据 ...

  4. Windows 本地文件搜索神器

    Wox: Windows 本地文件搜索神器 下载地址: https://github.com/Wox-launcher/Wox 注: Wox只能搜索C盘下的文件,所以需要结合everything 如果 ...

  5. python基础知识-12-模块的了解

    python其他知识目录 1.模块介绍: Python 模块(Module),是一个 Python 文件,以 .py 结尾,包含了 Python 对象定义和Python语句.模块让你能够有逻辑地组织你 ...

  6. TeamWork#3,Week5,Scrum Meeting 11.13

    最近我们根据之前发现的问题, 补充了相关知识,正在努力修复出现的问题,调整程序结构. 成员 已完成 待完成 彭林江 之前没有考虑到网站信息更新导致的程序可变性,正在调整爬虫程序结构 更换爬虫结构 郝倩 ...

  7. 45度炸队Alpha冲刺博客集

    博客集如下: Alpha冲刺Day1:第一天冲刺记录 Alpha冲刺Day2:第二天冲刺记录 Alpha冲刺Day3:第三天冲刺记录 Alpha冲刺Day4:第四天冲刺记录 Alpha冲刺Day5:第 ...

  8. java的小学生四则运算

    import java.awt.*; import java.awt.event.*; import java.io.FileNotFoundException; import java.io.IOE ...

  9. 在新的电脑上部署 Hexo,保留原有博客的方法

    用U盘从旧的电脑拷贝整个blog文件夹. 在新的电脑上装好git并配置好用户名和密钥. 安装 node.js 安装 hexo:npm install hexo-cli -g 用U盘把blog文件夹拷贝 ...

  10. 将通过<input type="file">上传的txt文件存储在localStorage,提取并构建File对象

    参考博文: JS 之Blob 对象类型 在本地存储localStorage中保存图片和文件 <input type="file" id="jobData" ...