当前位置:网站首页>[DFS "want" or "don't"] seek subsets; Seeking combination
[DFS "want" or "don't"] seek subsets; Seeking combination
2022-06-12 03:00:00 【I'm not xiaohaiwa~~~~】

Find all subsets of a sequence .
General train of thought :
Also use dfs To do the , Because it's actually “ want ” or “ Don't ” The problem of , This is a dfs Typical problems that can be solved !
Be careful :sort(start,end, Sorting method ), Generally speaking end=start+size(), however vector Yes .begin and .end() Just use the pointer .
vector Of push_back Is to add elements to the end , that pop_back Delete the last element !
AC Code :
class Solution {
public:
vector<vector<int> > subsets(vector<int> &S) {
vector<vector<int> > subs;
vector<int> sub;
sort(S.begin(),S.end());
dfs(S,subs,sub,0);
return subs;
}
void dfs(vector<int> S,vector<vector<int> >&subs,vector<int> sub,int start)
{
subs.push_back(sub);
for(int i=start;i<S.size();i++)
{
sub.push_back(S[i]); // want
dfs(S,subs,sub,i+1);
sub.pop_back(); // Don't ( Pay attention to this sub It's value passing , therefore pop_back Can )
}
}
};
Empathy ,“ want ” or “ Don't ” Combinatorial problem :
Here you are. n,k, Output 1~n in k Combination of numbers .
class Solution {
public:
vector<vector<int> > res;
vector<vector<int> > combine(int n, int k) {
if(n<k)
return res;
if(n==0 || k==0)
return res;
vector<int> cur;
for(int i=1;i<=n;i++)
{
cur.push_back(i);
dfs(n,k,i,1,cur);
cur.pop_back();
}
return res;
}
void dfs(int n,int k,int start,int times,vector<int> cur) //start The current number ,times What is the current number
{
//start Has joined cur
if(times==k)
{
res.push_back(cur);
return;
}
for(int i=start+1;i<=n;i++)
{
// want
cur.push_back(i);
dfs(n,k,i,times+1,cur);
// Don't
cur.pop_back();
}
}
};
边栏推荐
- What are the solutions across domains?
- laravel 8 选用 jwt 进行接口验证
- Comparison of scores
- 微信小程序项目实例——体质计算器
- Hypergraph tilted data is merged into root node and transferred to 3dfiles
- errno: -4078, code: ‘ECONNREFUSED‘, syscall: ‘connect‘, address: ‘127.0.0.1‘, port: 3306;postman报错
- Intel case
- Introduction to architecture - who moved my cake
- Paper recommendation: relicv2, can the new self supervised learning surpass supervised learning on RESNET?
- Demand and business model innovation - demand 10- observation and document review
猜你喜欢

微信小程序项目实例——我有一支画笔(画画)

Intel case

字符串处理:

一起教育科技单季营收2.3亿:同比降51% 净亏大幅收窄

A single quarter of educational technology revenue of 230million: a year-on-year decrease of 51% and a sharp narrowing of net loss

余压监控系统在高层民用建筑的应用

Paper recommendation: relicv2, can the new self supervised learning surpass supervised learning on RESNET?

Laravel 8 selects JWT for interface verification

如何防止商場電氣火灾的發生?

cupp字典生成工具(同类工具还有crunch)
随机推荐
Demand and business model innovation - demand 6- stakeholder analysis and hard sampling
GeForce GTX 2050/2080/3090/A6000自动安装nvidia显卡驱动
Intel case
cupp字典生成工具(同类工具还有crunch)
The market value has exceeded $3trillion. Why should apple, which has been criticized as a loser, rise again and again?
跨域有哪些解决方法?
Restful interface design specification [for reference only]
Wave view audio information
架构入门讲解 - 谁动了我的蛋糕
How to make div 100% page (not screen) height- How to make a div 100% of page (not screen) height?
2020-12-06
String number with special style
(9) Serial port interrupt
WPS表格 学习笔记 - 高亮显示重复值
errno: -4091, syscall: ‘listen‘, address: ‘::‘, port: 8000
2020-12-10
Demand and business model innovation - demand 10- observation and document review
One article to show you how to understand the harmonyos application on the shelves
Depth copy
How to build urban smart bus travel? Quick code to answer