当前位置:网站首页>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();
}
}
}
边栏推荐
- leetcode:5. Longest palindrome substring [DP + holding the tail of timeout]
- 人脸识别应用解析
- C#/VB. Net to delete watermarks in word documents
- PCL 常用拟合模型及使用方法
- 记一次JAP查询导致OOM的问题分析
- 如何设计好接口测试用例?教你几个小技巧,轻松稿定
- postgresql之整体查询大致过程
- 真实项目,用微信小程序开门编码实现(完结)
- The panel floating with the mouse in unity can adapt to the size of text content
- Application analysis of face recognition
猜你喜欢
Web3's need for law
Remember the problem analysis of oom caused by a Jap query
Digital scrolling increases effect
KYSL 海康摄像头 8247 h9 isapi测试
服装企业部署MES管理系统的五个原因
哈希表及完整注释
Classify the features of pictures with full connection +softmax
Niuke programming problem -- double pointer of 101 must be brushed
Application analysis of face recognition
postgresql之整体查询大致过程
随机推荐
3--新唐nuc980 kernel支持jffs2, Jffs2文件系统制作, 内核挂载jffs2, uboot网口设置,uboot支持tftp
真实项目,用微信小程序开门编码实现(完结)
Derivative, partial derivative, directional derivative
The third season of ape table school is about to launch, opening a new vision for developers under the wave of going to sea
Use of fiddler
CSDN summer camp course project analysis
QT常见概念-1
Code line breaking problem of untiy text box
Hash table and full comments
代码调试core-踩内存
Douban average 9 x. Five God books in the distributed field!
C#/VB.NET 删除Word文档中的水印
4 -- Xintang nuc980 mount initramfs NFS file system
C # / vb. Net supprime le filigrane d'un document word
4--新唐nuc980 挂载initramfs nfs文件系统
[C # notes] reading and writing of the contents of text files
Application analysis of face recognition
[leetcode]Search for a Range
Read fast RCNN in one article
Compress JS code with terser