42. Subsets && Subsets II
Subsets
Given a set of distinct integers, S, 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 S = [1,2,3]
, a solution is:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
思想: 顺序读,取前面的每个子集,把该位置数放后面作为新的子集。
class Solution {
public:
vector<vector<int> > subsets(vector<int> &S) {
sort(S.begin(), S.end());
vector<vector<int> > vec(1);
for(size_t id = 0; id < S.size(); ++id) {
int n = vec.size();
while(n-- > 0) {
vec.push_back(vec[n]);
vec.back().push_back(S[id]);
}
}
return vec;
}
};
Subsets II
Given a collection of integers that might contain duplicates, S, 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 S = [1,2,2]
, a solution is:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]
思想: 排序后,按照 1 的方法。但是若前面的数字与本数字相同,则只读取含有前面数字的每个子集,把自身放在后面作为一个新的子集。
class Solution {
public:
vector<vector<int> > subsetsWithDup(vector<int> &S) {
sort(S.begin(), S.end());
vector<vector<int> > vec(1);
size_t prePos, endTag;
prePos = endTag = 0;
for(size_t id = 0; id < S.size(); ++id) {
if(id > 0 && S[id] != S[id-1]) endTag = 0;
else endTag = prePos;
size_t n = vec.size();
prePos = n;
while(n > endTag) {
--n;
vec.push_back(vec[n]);
vec.back().push_back(S[id]);
}
}
return vec;
}
};
42. Subsets && Subsets II的更多相关文章
- LeetCode:Subsets I II
求集合的所有子集问题 LeetCode:Subsets Given a set of distinct integers, S, return all possible subsets. Note: ...
- 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 ...
- Subsets,Subsets II
一.Subsets Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a s ...
- <LeetCode OJ> 78 / 90 Subsets (I / II)
Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must ...
- 二分查找 BestCoder Round #42 1002 Gunner II
题目传送门 /* 题意:查询x的id,每次前排的树倒下 使用lower_bound ()查找高度,f[i]记录第一棵高度为x树的位置,查询后+1(因为有序) */ #include <cstdi ...
- [Swift]LeetCode78. 子集 | Subsets
Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The solut ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
随机推荐
- HDU 1171 背包
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- ExtJs中gridpanel分组后组名排序
/** * 定义降序的groupingStore */ var DescGroupingStore = Ext.extend(Ext.data.GroupingStore, { groupDir : ...
- iOS开发资源整理【01】
一.网站 Code4App 开发者常用库分享网站 GitHub git是一个优秀的分布式版本控制系统 stackoverflow 技术在线问答网站 CocoaChi ...
- springmvc跳转的几种方式
1:spring mvc 是围绕着DispatcherServlet展开的 ,其底层还是servlet 跳转方式: ①request.getRequestDispatcher("../ind ...
- 2014年4月份第3周51Aspx源码发布详情
WPY净水机网站源码 2014-4-14 [VS2008]源码描述: 实现产品展示,在线留言,信息发布,在线咨询,营销网络地图. 网站基本管理:网站banner管理 管理首页滚动图片信息 网站右下部 ...
- jQuery固定浮动侧边栏(jQuery fixed Sidebar)
这个功能现在应用的非常普遍,如果页面比较高,当滚动条拖到页面的下面的时候,侧边栏会出现一个固定跟随浏览器的DIV框,现思路是这样的:首先获取需要跟随的DIV距离页面顶部的距离,然后判断,当浏览器滚动的 ...
- HDU 3709 Balanced Number
发现只要Σa[i]*i%Σa[i]==0就可以. #include<iostream> #include<cstdio> #include<cstring> #in ...
- UVA 10407 差分思想的运用
就是每两项相减,肯定能被模数整除. #include <iostream> #include <cstring> #include <cstdio> #includ ...
- ”isEqual“ ”isEqalToString“ 和“==”三者区别
isEqual :首先判断的时对象类型是否 一样,然后再判断具体内容是否一致:如果类型不一样,return no: isEqualToString: 直接判断字符串内容,便捷更快速,但是前提确保比较的 ...
- [URAL]刷题记录表
URAL 1001. A + B 1002. 简单题,开方计算! 1003.