subset II
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],
[]
]
方法一:迭代
class Solution {
public:
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
sort(nums.begin(), nums.end());
vector<int> v;
vector<vector<int>> res; res.push_back(v); //empty set std::size_t prev_size = ;
for(std::size_t i=;i<nums.size();i++)
{
std::size_t size = res.size();
for(std::size_t j=;j<size;j++)
{
if(i== || nums[i] != nums[i-] || j >= prev_size)
{
v.assign(res[j].begin(),res[j].end());
v.push_back(nums[i]);
res.push_back(v);
}
}
prev_size = size;
}
return res;
}
};
方法二:递归
#include <vector>
#include <algorithm>
using std::vector; class Solution {
public:
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
sort(nums.begin(), nums.end());
vector<int> v;
vector<vector<int>> res;
subsetsWithDupCore(res, , nums, v);
return res;
}
private:
void subsetsWithDupCore(vector<vector<int>>& res,int start,vector<int>& nums,vector<int>& v)
{
if(start == nums.size())
{
res.push_back(v);
return;
}
if(v.size() == || nums[start] != v[v.size()-])
{
subsetsWithDupCore(res, start+, nums, v);
} v.push_back(nums[start]);
subsetsWithDupCore(res, start+, nums, v);
v.pop_back();
}
};
subset II的更多相关文章
- [Leetcode 90]求含有重复数的子集 Subset II
[题目] Given a collection of integers that might contain duplicates, nums, return all possible subsets ...
- 子集系列(一) 传统subset 问题,例 [LeetCode] Subset, Subset II, Bloomberg 的一道面试题
引言 Coding 问题中有时会出现这样的问题:给定一个集合,求出这个集合所有的子集(所谓子集,就是包含原集合中的一部分元素的集合). 或者求出满足一定要求的子集,比如子集中元素总和为定值,子集元素个 ...
- Subset II leetcode java
题目: Given a collection of integers that might contain duplicates, S, return all possible subsets. No ...
- [Leetcode 40]组合数和II Combination Sum II
[题目] Given a collection of candidate numbers (candidates) and a target number (target), find all uni ...
- [Leetcode 78]求子集 Subset
[题目] Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The ...
- 子集系列(二) 满足特定要求的子集,例 [LeetCode] Combination, Combination Sum I, II
引言 既上一篇 子集系列(一) 后,这里我们接着讨论带有附加条件的子集求解方法. 这类题目也是求子集,只不过不是返回所有的自己,而往往是要求返回满足一定要求的子集. 解这种类型的题目,其思路可以在上一 ...
- 九章lintcode作业题
1 - 从strStr谈面试技巧与代码风格 必做题: 13.字符串查找 要求:如题 思路:(自写AC)双重循环,内循环读完则成功 还可以用Rabin,KMP算法等 public int strStr( ...
- [Leetcode 39]组合数的和Combination Sum
[题目] Given a set of candidate numbers (candidates) (without duplicates) and a target number (target) ...
- [Leetcode 216]求给定和的数集合 Combination Sum III
[题目] Find all possible combinations of k numbers that add up to a number n, given that only numbers ...
随机推荐
- tomcat-java_opts设置说明
The JAVA_OPTS environment variable can be used to specify additional arguments to the JVM JBoss will ...
- tomcat7-maven-plugin 端口
不知道有没有人像我这样,在pom配置了下面这段之后, <plugins> <plugin> <groupId>org.apache.tomcat.maven< ...
- scvmm sdk之powershell(一)
shell表示计算机操作系统中的壳层,与之相对的是内核,内核不能与用户直接交互,而是通过shell为用户提供操作界面,shell分为两类,一种提供命令行界面,一种提供图形界面.windows powe ...
- [react001] 使用webpack自动构建react 项目
1.react 简介 React 是一个Facebook出品的前端UI开发框架.react官方的tutorials 为了让人容易上手,并没有给在平常工作使用react的详细配置,随意学习的深入,你为了 ...
- Java异常:选择Checked Exception还是Unchecked Exception?
http://blog.csdn.net/kingzone_2008/article/details/8535287 Java包含两种异常:checked异常和unchecked异常.C#只有unch ...
- subprocess.Popen命令如何隐藏弹框
在用PYQT编写GUI界面时,代码中有用到subprocess.Popen(),打包exe后每次遇到subprocess语句是就会弹出命令框,很是头疼, 下面是解决的办法 import subproc ...
- 在相应目录下新建或读取xml文件
string path = AppDomain.CurrentDomain.BaseDirectory+"UserContent1.xml"; //判断相应路径下文件是否存在 不存 ...
- 总结目前为止学到的关键字(break,continue,private,static,this,super,final,abstract)
1.控制跳转语句:break(结束) 使用的场景: a.循环当中 b.switch break关键字需要注意的问题: 1.break关键字只能用于循环和switch语句当中,其本质就是结束整段语句的意 ...
- SpringBoot :docker
Docker 是一个开源的应用容器引擎 docker官方网站:https://hub.docker.com/ $部署docker到Linux系统 1.准备一个Linux系统的虚拟机或者物理机 本例所使 ...
- win7 virtio 驱动下载
下载地址: https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/archive-virtio/