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 ...
随机推荐
- c++英文单词频度统计程序
英文单词频度统计程序(c++版) 写一个程序,分析一个文本文件(英文文章)中各个次出现的频率,并且把频率最高的十个词打印出来. 分析过程: (1) 简单设想大致分为两大步骤: 1.经过文本文件的读操 ...
- Mike的农场 (BZOJ 4177)
题目大意: 给N个东西分AB类,分到A类和B类分别得到相应的钱记为A[i],B[i],然后有一些冲突关系<x,y,z>,如果物品x,y不同类需要付出z的钱.还有一些外快<S,x,y& ...
- Linux I2C总线设备驱动模型分析(ov7740)
1. 框架1.1 硬件协议简介1.2 驱动框架1.3 bus-drv-dev模型及写程序a. 设备的4种构建方法a.1 定义一个i2c_board_info, 里面有:名字, 设备地址 然后i2c_r ...
- php函数的可变参数
<?php function add() { $arr = func_get_args(); //func_num_args() $sum =0; for($i=0;$i<count($a ...
- ubuntu下命令行打开pdf/doc/ppt文件
1 打开pdf evince *.pdf 2 打开ppt libreoffice *.ppt3 打开doc libreoffice *.doc
- Android之View和SurfaceView
Android之View和SurfaceView Android游戏当中主要的除了控制类外就是显示类View.SurfaceView是从View基类中派生出来的显示类.android游戏开发中常用的三 ...
- [计算机、网络相关历史]unix简史
本文2001年由台湾“网络农夫”所写,其人生平不祥,此文受鸟哥大力推崇,两人应该相识.文章写得很不错,应该是查了很多资料整理而成的,美中不足的是好多语句不通顺,国考语文绝对不及格,哈哈. 0.我的准备 ...
- Python print格式化输出
python中的print格式化输出,基本格式:"[字符串]%格式1[字符串]%格式2[字符串]....."%(string1,string2.....) 格式符号 ------- ...
- Oracle GoldenGate for Big Data 12.2.0.1的新特性
ogg for bigdata 12.2已经发布,新增有如下特性:支持java replicat进程OGG12.2中开发了基于java的replicat模式,以前的版本是基于extract进程中使用u ...
- iOS 中捕获程序崩溃日志
iOS 中捕获程序崩溃日志 (2014-04-22 17:35:59) 转载▼ iOS开发中遇到程序崩溃是很正常的事情,如何在程序崩溃时捕获到异常信息并通知开发者,是大多数软件都选择的方法.下 ...