LeetCode 90:Subsets II
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],
[]
]
Subscribe to see which companies asked this question
这题有反复元素,但本质上,跟上题非常相似。也能够用相似的方法处理:
class Solution {
public:
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
vector<vector<int>> ans(1, vector<int>());
sort(nums.begin(), nums.end());
int pre_size = 0;
for (int i = 0; i < nums.size(); i++)
{
int n = ans.size();
for (int j = 0; j < n; j++) //第二个for循环是为了求得包括nums[i]的全部子集
{
if (i == 0 || nums[i] != nums[i -1] || j>=pre_size)
{
ans.push_back(ans[j]); //在尾部又一次插入ans已经存在的子集
ans.back().push_back(nums[i]); //将nums[i]加入进去
}
}
pre_size = n;
}
return ans;
}
};
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
LeetCode 90:Subsets II的更多相关文章
- LeetCode OJ:Subsets II(子集II)
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
- LeetCode OJ:Subsets(子集)
Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must ...
- LeetCode(90) Subsets II
题目 Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...
- lintcode 中等题:subsets II 带重复元素的子集
题目 带重复元素的子集 给定一个可能具有重复数字的列表,返回其所有可能的子集 样例 如果 S = [1,2,2],一个可能的答案为: [ [2], [1], [1,2,2], [2,2], [1,2] ...
- [LeetCode 题解]: Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- LeetCode OJ:Permutations II(排列II)
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 【LeetCode 90】子集 II
题目链接 [题解] 我们在枚举下一个要取哪个数字的时候. 如 1112233 for (int i = start;i<=n;i++) //其中start-1是上一次取的位置. 如果i>s ...
- 90. Subsets II
题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...
- Leetcode之回溯法专题-90. 子集 II(Subsets II)
Leetcode之回溯法专题-90. 子集 II(Subsets II) 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入 ...
随机推荐
- bootstrap之排版样式
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- DDD模型领域WF到领域层(十五)
实现超市的结算系统: 计算相应的优惠方式的接口 public interface ICompute { double GetResultTotalMoney(double TotalMoney); } ...
- PIGS
题解: 考虑建立一个分层图,从s向猪圈连边,容量为初始容量, 猪圈向第一个顾客连边,容量为INF 顾客向汇点连边,容量为购买量 这样一轮就搞完了,考虑下一个顾客 由于上一轮被顾客访问的猪圈都互通了,那 ...
- 【AtCoder】AISing Programming Contest 2019
本来以为是1199rated的..仔细一看发现是1999,所以就做了一下 这场涨分很轻松啊...为啥又没打 等pkuwc考完我一定打一场atcoder(咕咕咕,咕咕咕,咕咕咕咕咕咕咕~) 但是其实我思 ...
- python 字符串组成MySql 命令时,字符串含有单引号或者双引号导致出错解决办法
引用自:https://blog.csdn.net/zhaoya_huangqing/article/details/48036839 一.在组成SQL语句并发送命令时完全按照Python中的样式去传 ...
- MT4编程初级手册
http://www.fxunion.com/college/2015/17554.html
- Minimum Transport Cost HDU1385(路径打印)
最短路的路径打印问题 同时路径要是最小字典序 字典序用floyd方便很多 学会了两种打印路径的方法!!! #include <stdio.h> #include <string.h& ...
- HDU 2612 find a way 【双BFS】
<题目链接> 题目大意:两个人分别从地图中的Y 和 M出发,要共同在 @ 处会面(@不止有一处),问这两个人所走距离和的最小值是多少. 解题分析: 就是对这两个点分别进行一次BFS,求出它 ...
- window下mongodb安装和配置
mongodb安装和配置 1.下载:https://www.mongodb.com 2.解压到盘的根目录下,本人解压到D盘根目录 3.在软件根目录下新建一个文件夹data 4.再新建两个文件夹db.l ...
- Python Django 学习 (二) 【Django 模型】
注: 由于自己排版确实很难看,本文开始使用markdown编辑,希望有所改善 官方定义 A model is the single, definitive source of information ...