[Leetcode Week8]Subsets II
Subsets II 题解
原创文章,拒绝转载
题目来源:https://leetcode.com/problems/subsets-ii/description/
Description
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).
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],
[]
]
Solution
class Solution {
public:
vector<vector<int> > subsetsWithDup(vector<int>& nums) {
vector<vector<int> > result;
if (nums.empty())
return result;
result.resize(1);
sort(nums.begin(), nums.end());
int lastNum = nums[0], size = 1;
int i, j;
for (i = 0; i < nums.size(); i++) {
if (lastNum != nums[i]) {
lastNum = nums[i];
size = result.size();
}
int newResSize = result.size();
for (j = newResSize - size; j < newResSize; j++) {
result.push_back(result[j]);
result.back().push_back(nums[i]);
}
}
return result;
}
};
解题描述
这道题是求幂集的升级版:原始集合含有重复元素,生成幂集的过程中要去重。这道题在第一版的算法基础上,对重复数字开始的地方进行区分(记录不应重复添加新数字的集合数目),以避免对前面产生的数组再全部进行新数字的添加而导致重复的集合的出现。
[Leetcode Week8]Subsets II的更多相关文章
- 【leetcode】Subsets II
Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...
- [LeetCode] 90.Subsets II tag: backtracking
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- [leetcode]90. Subsets II数组子集(有重)
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- Java for LeetCode 090 Subsets II
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
- [LeetCode] 90. Subsets II 子集合 II
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- 深度优先搜索算法(DFS)以及leetCode的subsets II
深度优先搜索算法(depth first search),是一个典型的图论算法.所遵循的搜索策略是尽可能“深”地去搜索一个图. 算法思想是: 对于新发现的顶点v,如果它有以点v为起点的未探测的边,则沿 ...
- LeetCode 90. Subsets II (子集合之二)
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
- [Leetcode Week8]Subsets
Subsets 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/subsets/description/ Description Given a set ...
- [LeetCode] 90. Subsets II 子集合之二
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
随机推荐
- 关于Vue脚手架写法的问题
问题描述: main.js import Vue from 'vue' import App from './App' /* eslint-disable no-new */ new Vue({ el ...
- MySQL☞dual虚拟表
Dual表:虚拟表,专门用来测试各种函数:(本来以为跟Oracle中的dual表一样,发现还是不太一样)
- selenide 自动化UI测试中Configuration全局配置项目
selenide 在测试过程中需要设置许多的默认值,方便在测试过程中进行和很好的使用.下面我们在selenide中的api引用过来看看! static Configuration.AssertionM ...
- 【java并发编程实战】第五章:基础构建模块
1.同步容器类 它们是线程安全的 1.1 vector和hashtable. 和Collections.synchronizeXxx()一样.实现方式就是在每个方法里面加入synchronize代码块 ...
- Python全栈 MongoDB 数据库(概念、安装、创建数据)
什么是关系型数据库? 是建立在关系数据库模型基础上的数据库,借助于集合代数等概念和方法来处理数据库中的数据, 同时也是一个被组织成一组拥有正式描述性的表格( ...
- spring boot接口 支持https
1.拥有证书,可自己生成测试用javatool生成 keytool -keystore [keyname].jks -genkey -alias tomcat -keyalg RSA 接下来输入相关信 ...
- Daily Scrum02 11.29
今天大家都已经开始了进行第二轮迭代的工作!相比第一轮迭代,每个人都已经有了一定开发经验,这次做起来顺手很多.薛神和肖犇的挑战最大,他们需要实现好友功能,手机间的通信.服务器的搭建都是难点,但他们的热情 ...
- C++-STL:vector用法总结
目录 简介 用法 1. 头文件 2. vector的声明及初始化 3. vector基本操作 简介 vector,是同一类型的对象的集合,这一集合可看作可变大小的数组,是顺序容器的一种.相比于数组,应 ...
- lintcode-125-背包问题 II
125-背包问题 II 给出n个物品的体积A[i]和其价值V[i],将他们装入一个大小为m的背包,最多能装入的总价值有多大? 注意事项 A[i], V[i], n, m均为整数.你不能将物品进行切分. ...
- PAT 1030 完美数列
https://pintia.cn/problem-sets/994805260223102976/problems/994805291311284224 给定一个正整数数列,和正整数 p,设这个数列 ...