Subsets,Subsets II
一.Subsets
Given a set of distinct integers, 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,3]
, a solution is:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
class Solution {
public:
void dfs(vector<int>& nums ,int numsSize,int startPos,vector<vector<int>>& res,vector<int>& oneOfRes)
{
for(int i=startPos;i<numsSize;i++){
oneOfRes.push_back(nums[i]);
res.push_back(oneOfRes);
dfs(nums,numsSize,i+,res,oneOfRes);
oneOfRes.pop_back();
}
}
vector<vector<int>> subsets(vector<int>& nums) {
sort(nums.begin(),nums.end());
vector<vector<int>> res;
vector<int> oneOfRes;
res.push_back(vector<int>());
int numsSize = nums.size();
dfs(nums,numsSize,,res,oneOfRes);
return res;
}
};
二.SubsetsII
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:
void dfs(vector<int>& nums,int numsSize,int startPos,vector<vector<int>>& res,vector<int>& oneOfRes)
{
for(int i=startPos;i<numsSize;i++){
if(i>startPos && nums[i]==nums[i-]){
continue;
}
oneOfRes.push_back(nums[i]);
res.push_back(oneOfRes);
dfs(nums,numsSize,i+,res,oneOfRes);
oneOfRes.pop_back();
}
}
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
sort(nums.begin(),nums.end());
vector<vector<int>> res;
vector<int> oneOfRes;
int numsSize = nums.size();
res.push_back(oneOfRes);
dfs(nums,numsSize,,res,oneOfRes);
return res;
}
};
Subsets,Subsets II的更多相关文章
- LeetCode:Subsets I II
求集合的所有子集问题 LeetCode:Subsets Given a set of distinct integers, S, return all possible subsets. Note: ...
- 42. Subsets && Subsets II
Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset ...
- leetcode -day31 Subsets I II
1. Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a ...
- Subsets I&&II——经典题
Subsets I Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a s ...
- LeetCode Subsets I& II——递归
I Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must ...
- <LeetCode OJ> 78 / 90 Subsets (I / II)
Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must ...
- [Swift]LeetCode78. 子集 | Subsets
Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The solut ...
- 【leetcode】Subsets II
Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...
- 90. Subsets II
题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...
随机推荐
- web前端的学习误区
web前端的学习误区 网页制作是计算机专业同学在大学期间都会接触到的一门课程,而学习网页制作所用的第一个集成开发环境(IDE)想必大多是Dreamweaver,这种所见即所得的“吊炸天”IDE为我们 ...
- 简单的FTP上传下载(java实现 swing界面)
/** *阅读前请自己在win7上建立FTP主机 *具体步骤如:http://jingyan.baidu.com/article/574c5219d466c36c8d9dc138.html * 然后将 ...
- linux定时执行
/root/crontab-conf文件为root用户定时执行计划文件 命令:crontab -l 说明:列出定时执行的计划列表 命令:crontab -e 说明:编辑定时执行的计划文件 ...
- 你想不到的IT运维前途
本人一毕业就走上了IT系统运维的道路,我之所以踏上这条路并一直坚持了下来,因为觉得运维工作并非一味关注技术,而是关注包括技术在内的更综合的解决方案,也就是说,做运维,自己要学的知识面更广,考虑问题要更 ...
- $ cd `dirname $0` 和PWD%/* shell变量的一些特殊用法
在命令行状态下单纯执行 $ cd `dirname $0` 是毫无意义的.因为他返回当前路径的".". $0:当前Shell程序的文件名dirname $0,获取当前Shell程序 ...
- javascript事件委托和jQuery事件绑定on、off 和one
一. 事件委托什么是事件委托?用现实中的理解就是:有100 个学生同时在某天中午收到快递,但这100 个学生不可能同时站在学校门口等,那么都会委托门卫去收取,然后再逐个交给学生.而在jQuery 中, ...
- windows下开发PHP扩展(无需Cygwin)
第一步:准备 1.php源码包和windows下的二进制包,以及安装Visual C++,并把Microsoft Visual Studio/Common/MSDev98/Bin的绝对路径添加到win ...
- Centos安装webbench
webbench最多可以模拟3万个并发连接去测试网站的负载能力,个人感觉要比Apache自带的ab压力测试工具好,安装使用也特别方便. 1.适用系统:Linux 2.编译安装: 引用 wget htt ...
- 2014第8周一JS正则小问题
今天解决一个关于JS正则表达式的小问题,需求是匹配6位或9位数字,我原来的写法是这样的/^(/d){6}|(/d){9}$/.test(val),但测试发现输入1234567时也返回成功,很郁闷搜索了 ...
- 你真的用上keepalive了吗
转自http://qa.blog.163.com/blog/static/19014700220134771052763/ Keep-Alive即俗称的长连接,使客户端到服务端建立的连接持续有效,当对 ...