leetcode -day31 Subsets I II
1、
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> >* v;
vector<vector<int> > subsets(vector<int> &S) {
v = new vector<vector<int> >();
//先排序
sort(S.begin(),S.end());
vector<int> res;
generate(res, S, 0);
return *v;
}
//对每个元素有放与不放两种选择
void generate(vector<int> res, vector<int> &S, int i)
{
if(i == S.size())
{
v->push_back(res);
return;
}
else
{
generate(res, S, i+1);//不放当前元素
res.push_back(S[i]); //放入当前元素
generate(res, S, i+1);
}
}
};
2、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],
[]
]
分析:此题和上题类似,就是有了反复元素,想法也是先进行排序,排序后,从头到尾遍历,记录每一个元素的个数,每一个子集中有0-i个指定元素(一共i个),代码例如以下:
class Solution {
public:
vector<vector<int> >* v;
vector<vector<int> > subsetsWithDup(vector<int> &S) {
v = new vector<vector<int> >();
//先排序
sort(S.begin(),S.end());
vector<int> res;
generate(res, S, 0,0,0);
return *v;
}
//pre: 排序后前一个元素 num: 前一个元素出现的次数
void generate(vector<int> res, vector<int> &S, int i,int pre,int num)
{
if(i == S.size())
{
v->push_back(res);
for(int j=1; j<=num; ++j){
res.push_back(pre); //放入之前元素
v->push_back(res);
}
return;
}
else if(pre != S[i] || num < 1 ) //与之前元素不同或者是首次
{
if(num < 1){
generate(res,S,i+1,S[i],1);
}else{
generate(res, S, i+1,S[i],1);//放入0个元素
for(int j=1; j<=num; ++j){
res.push_back(pre);
generate(res, S, i+1,S[i],1);//放入i个元素后从当前位置開始
}
}
}else{
pre = S[i];
generate(res, S, i+1,pre,num+1);
}
}
};
版权声明:本文博客原创文章,博客,未经同意,不得转载。
leetcode -day31 Subsets I II的更多相关文章
- [LeetCode] 90.Subsets II tag: backtracking
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- [leetcode]90. Subsets II数组子集(有重)
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- Java for LeetCode 090 Subsets II
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
- [LeetCode] 90. Subsets II 子集合 II
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- LeetCode Single Number I / II / III
[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- LeetCode 137. Single Number II(只出现一次的数字 II)
LeetCode 137. Single Number II(只出现一次的数字 II)
- LeetCode:路径总和II【113】
LeetCode:路径总和II[113] 题目描述 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例:给定如下二叉树, ...
- LeetCode:组合总数II【40】
LeetCode:组合总数II[40] 题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candi ...
随机推荐
- IOS应用上传须要做的工作
苹果开发人员 https://developer.apple.com/ 证书创建流程 certificates (证书): 是电脑可以增加开发人员计划的凭证 证书分为:开发证书和公布(产品)证书, ...
- 让struts2和servlet共存
由于struts2默认的是拦截全部的请求 由配置文件能够看出 <filter> <filter-name>struts2</filter-name> <fil ...
- ueditor使用注意事项
1.js问题的介绍 第一ueditor型材 <script type="text/javascript" src="ueditor1_4_3-utf8-jsp/ue ...
- UVA 847 - A Multiplication Game(游戏)
UVA 847 - A Multiplication Game 题目链接 题意:一个数一開始是1,每次轮流乘2-9,谁先大于n谁就赢,问谁胜 思路:博弈,找出必胜态.2-9为stan,10-18为ol ...
- ThreadPoolExecutor的一点理解
整个ThreadPoolExecutor的任务处理有4步操作: 第一步,初始的poolSize < corePoolSize,提交的runnable任务,会直接做为new一个Thread的参数, ...
- 于SharePoint经营SharePoint Designer建立
于SharePoint经营SharePoint Designer建立 SharePoint Designer 2010(SPD)它是一种强大的工具,帮助建立一个高速解决方案. 通过连接到现场,能够自由 ...
- Java多播通讯框架 JGroups(转)
JGroups是一个可靠的群组通讯Java工具包.它基于IP组播(IP multicast),但在可靠性,组成员管理上对它作了扩展. JGroups的可靠性体现在: 1,对所有接收者的消息的无丢失传输 ...
- -ms-grid -ms-grid-rows -ms-grid-row -ms-grid-columns -ms-grid-column
style: display:-ms-grid-ms-grid-columns和-ms-grid-rows的值可以为: >标准长度单位,如像素 >对象宽度(对于列)或高度(对于行)的百分比 ...
- DeviceIoControl的使用说明
应用程序和驱动程序的通信过程是:应用程序使用CreateFile函数打开设备,然后用DeviceIoControl与驱动程序进行通信,包含读和写两种操作.还能够用ReadFile读数据用WriteFi ...
- Java之旅(三)--- JSTL和EL表情
先给大家看一段JSP的代码.看看有什么感受? <% List<UsEL> usELList = pageModel.getList(); for (ItELator<Us ...