一.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的更多相关文章

  1. LeetCode:Subsets I II

    求集合的所有子集问题 LeetCode:Subsets Given a set of distinct integers, S, return all possible subsets. Note: ...

  2. 42. Subsets && Subsets II

    Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset ...

  3. leetcode -day31 Subsets I II

    1.  Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a ...

  4. Subsets I&&II——经典题

    Subsets I Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a s ...

  5. LeetCode Subsets I& II——递归

    I Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must ...

  6. &lt;LeetCode OJ&gt; 78 / 90 Subsets (I / II)

    Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must ...

  7. [Swift]LeetCode78. 子集 | Subsets

    Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The solut ...

  8. 【leetcode】Subsets II

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

  9. 90. Subsets II

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

随机推荐

  1. C#比较两个时间大小

    DateTime t1 = Convert.ToDateTime("2012-12-31 23:59:00");            DateTime t2 = Convert. ...

  2. Opencv--HoughCircles源码剖析

    图形可以用一些参数进行表示,标准霍夫变换的原理就是把图像空间转换成参数空间(即霍夫空间),例如霍夫变换的直线检测就是在距离-角度空间内进行检测.圆可以表示成: (x-a)2+(y-b)2=r2     ...

  3. 你想不到的IT运维前途

    本人一毕业就走上了IT系统运维的道路,我之所以踏上这条路并一直坚持了下来,因为觉得运维工作并非一味关注技术,而是关注包括技术在内的更综合的解决方案,也就是说,做运维,自己要学的知识面更广,考虑问题要更 ...

  4. 时间的获取和转换,C#和Sql

    我们可以通过使用DataTime这个类来获取当前的时间.通过调用类中的各种方法我们可以获取不同的时间:如:日期(2008-09-04).时间(12:12:12).日期+时间(2008-09-04 12 ...

  5. ORA-01034/ORA-27101解决

    sql> shutdown immediate 后就无法进行任何操作了,重新通过sqlplus不能登录,提示ORA-01034和ORA-27101错误 解决,以下全部在cmd中: 1. 启动or ...

  6. tomcat配置CATALINA_HOME变量

    1.CATALINA_HOME是TOMCAT安装路径的别名,目的是为了方便使用TOMCAT 2.计算机>属性>环境变量, 新建环境变量.变量名为CATALINA_HOME ,变量值tomc ...

  7. (jQuery||Zepto).extend 的一个小问题

    最近一直在搞移动端,也由于自己对jQuery比较熟悉,再加上Zepto提供了跟jQuery一样的API,所以就选择了Zepto作为开发框架. 由于是移动端开发,所以也应用了一些ES5新增的API,比如 ...

  8. MySQL高效获取记录总数

    通常mysql获取查询记录总数我们使用如下语句: SELECT COUNT(*) FROM users WHERE k='avs';  或:SELECT id FROM goods WHERE k=' ...

  9. Dragon Balls--hdu3635(并查集)

    Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  10. 【CKEditor ASP.NET】解决360安全浏览器极速模式下不显示

    博主问题只是出在误删了style.js文件 首先我用的是这种模式,在单个页面上导入: <%@ Register Assembly="CKEditor.NET" Namespa ...