当前位置:网站首页>LeetCode 77:组合
LeetCode 77:组合
2022-07-06 19:10:00 【斯沃福德】
链接
题目:
思路:回溯(无重复元素不可复选)


与子集类似,数据无重复,且(2,1)和(1,2)是一个结果,故不能复选;
子集问题中每一个节点都是子集,但组合问题中当元素个数到达k才将path放入res中!
注意:
集合问题中,给的是数组,for从0到n-1 ;组合问题给的n,for从1到n ;
Java实现:
class Solution {
List<List<Integer>> res=new LinkedList<>();
LinkedList<Integer> path=new LinkedList<>();
public List<List<Integer>> combine(int n, int k) {
dfs(n,k,1); // 注意start从1开始
return res;
}
void dfs(int n,int k,int start){
//终止条件,当元素个数为k则添加
if(path.size()==k){
res.add(new LinkedList(path));
}
for(int i=start;i<=n;i++){
//递归前做选择
path.add(i);
//递归
dfs(n,k,i+1);
//递归后撤销选择
path.removeLast();
}
}
}
边栏推荐
- C#/VB.NET 删除Word文檔中的水印
- How to build a 32core raspberry pie cluster from 0 to 1
- 用全连接+softmax对图片的feature进行分类
- 你不可不知道的Selenium 8种元素定位方法,简单且实用
- leetcode:736. LISP syntax parsing [flowery + stack + status enumaotu + slots]
- Halcon instance to opencvsharp (C openCV) implementation -- bottle mouth defect detection (with source code)
- Mmdetection3d loads millimeter wave radar data
- The third season of ape table school is about to launch, opening a new vision for developers under the wave of going to sea
- QPushButton-》函数精解
- 4 -- Xintang nuc980 mount initramfs NFS file system
猜你喜欢
随机推荐
Application analysis of face recognition
所谓的消费互联网仅仅只是做行业信息的撮合和对接,并不改变产业本身
3--新唐nuc980 kernel支持jffs2, Jffs2文件系统制作, 内核挂载jffs2, uboot网口设置,uboot支持tftp
Detailed explanation of line segment tree (including tested code implementation)
企业中台建设新路径——低代码平台
MySQL --- 常用函数 - 字符串函数
Summer Challenge database Xueba notes (Part 2)~
What are the applications and benefits of MES management system
leetcode:736. LISP syntax parsing [flowery + stack + status enumaotu + slots]
Qpushbutton- "function refinement"
wzoi 1~200
1 -- Xintang nuc980 nuc980 porting uboot, starting from external mx25l
The so-called consumer Internet only matches and connects industry information, and does not change the industry itself
差异与阵列和阵列结构和链表的区别
Planning and design of double click hot standby layer 2 network based on ENSP firewall
A new path for enterprise mid Platform Construction -- low code platform
Google Earth Engine(GEE)——Landsat 全球土地调查 1975年数据集
Douban average 9 x. Five God books in the distributed field!
Oracle中日期的使用方法实例
AWS学习笔记(一)









